Home python split the number to the components

# split the number to the components

Author

Date

Category

6-valued number is fed to the Input function. You need to split it on some numbers. For example: 123456 – & gt; 1 2 3 4 5 6. This is done in order to check the equivalence of the sum of the first and the second three of the numbers (the task of a happy ticket). In programming I am a beginner and preferably everything to make DIV, MOD operations, if-else

``````a = int (input ())
B = A // 1000
One = b // 100
Two = B% 11
Three = B% 10
C = A% 1000
FOUR = C // 100
Five = C% 11
SIX = C% 10
If (One + Two + Three) == (Four + Five + Six):
Print ('happy')
ELSE:
Print ('Normal')
``````

You can, of course, honestly calculate the remnants from division, etc., but the easiest way to smash the string to symbols, and then translate these characters back to numbers. This is done in the second line of the following program:

``````a = 123456
B = Map (int, str (a))
Print B.
IF Len (b) == 6:
Print "Happy" IF B  + B  + B  == b  + b  + b  else "unhappy"
``````

If you really want to be honestly divided by 10, you can replace the second line of this program to such a fragment:

``````b = []
While A & GT; 0:
B.APPEND (A% 10)
a = a // 10
B = B [:: - 1] # So you can deploy, if we were important
Print B.
``````

Now in the text of your program: for some reason, the residue from division by 11 (in the rows `Two = B% 11 `and `Five = C% 11 `, although it was correct It would be divided into 10, and then take the balance from dividing on 10 (`two = (b // 10)% 10 `). You need to figure out where 11 came from, as it is a very strange code.

Supplement:

If it is important for us to maintain cases of input values ​​`A `less than six-digit, then you can change only one line:

``````while len (b) & lt; 6: # Now in B will be deliberately 6 elements
``````

also recommend learning code from a neighboring answer (from JFS), since it has a number of interesting moments.

To find the amount of the number of numbers specified as a string:

``````def sum_digits (s):
RETURN SUM (MAP (INT, S))
``````

is an idiomatic way for this task in python.

`sum_digits () `function can be used to determine whether the specified number “happy” is:

``````#! / usr / bin / env python3
s = input ()
Middle = Len (s) // 2 # Middle
if middle == 0 or sum_digits (s [: middle]) == SUM_DIGITS (s [-middle:]):
Print ('happy')
ELSE:
Print ('Normal')
``````

The number is considered “happy” if the sum of the numbers of the left half is equal to the amount of numbers of the right half of the number. Empty string (no figures) – as a degenerate case – also considered “happy”.

If you want, you can use the division to smash a non-negative integer on individual numbers:

``````def digits_recursive (n, digits = []):
Return Digits_Recursive (N // 10, [N% 10] + Digits) IF N Else Digits OR 
``````

Example:

``````& gt; & gt; & gt; Digits_Recursive (123)
[1, 2, 3]
``````

or the same using an explicit cycle:

``````Def digits_iteRative (Nonneg):
Digits = []
While Nonneg:
Digits + = [Nonneg% 10]
Nonneg // = 10
RETURN DIGITS [:: - 1] OR 
``````

Example:

``````& gt; & gt; & gt; Digits_iteRative (123)
[1, 2, 3]
& gt; & gt; & gt; SUM (_)
6.
``````

saw this task in the introductory course of Python before studying arrays and functions like `MAP () `So in the case there was a correct way.

``````a = int (input ())
b = a // 1000
one = b // 100
two = b // 10% 10
three = b% 10
c = a% 1000
four = c // 100
five = c // 10% 10
six = c% 10
if (one + two + three) == (four + five + six):
print ('Happy')
else:
print ('Normal')
``````

``````num = list ('123321')
summ_1 = int (num ) + int (num ) + int (num ) # sum of the first three
summ_2 = int (num ) + int (num ) + int (num ) # sum of the last three three
``````

Here’s another joke that the task says that a string is being entered. And the task is solved quite simply.

``````a = input ()
b = int (a ) + int (a ) + int (a )
c = int (a ) + int (a ) + int (a )
if c == b:
print ('Happy')
else:
print ('Normal')
``````

``````n = input () # input
b = list (n) # make a list of numbers from this number (they will be saved as text)
c = [int (el) for el in b] # we make numbers from the text for each element of the list (you can look at the list generators on the Internet)
``````

``````a = int (input ())
summa = 0
forcha = 0
for i in range (3):
d = a // (10 ** ((len (str (a))) - 1))
a% = (10 ** ((len (str (a))) - 1))
summa + = d
for j in range (3):
g = a // (10 ** ((len (str (a))) - 1))
a% = (10 ** ((len (str (a))) - 1))
forcha + = g
if summa == forcha:
print ("Happy")
else:
print ("Not happy")
``````

If about a lucky ticket, then you can do this:

``````is_happy = lambda x: x% 1e3% 9 == x // 1e3% 9
``````

Then you can write something like:

``````is_happy (888978) # True
is_happy (265827) # False
``````

This is not a direct summation, but the so-called “root number”, when the numbers are summed recursively until there is only one left. But it works the same way.

If pairs are not 3 numbers, but 4 each, then instead of `1e3 `we write `1e4 `, if 2 each – respectively `1e2 `, well, so on.

If you need to write directly in Russian, then do it like this:

``````{True: 'Happy', False: 'Unhappy'} [is_happy (235631)]
# Or so, it will run faster, but the first option is more readable
is_happy (235631) and 'Happy' or 'Unlucky'
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.