So what I need to do is read in a number and transform every digit.
- add 2 to an odd digit
- subtract 3 from an even digit (watch out for negative numbers!)
- zero stays 0
input
14502
wanted output
31701
Current output
14504
Below is what I have for now, I can read every digit in a for loop but I don't know how to transorm them one by one.
num = int(input("Enter a number:"))
for digit in str(num):
print(digit)
if (num % 2)==0:
print(num 2)
else:
print(num - 3)
ALSO NO IMPORTS
CodePudding user response:
num = input("Enter a number:")
new_num = []
single_number = ''
for digit in num:
digit = int(digit)
if digit == 0:
new_num.append(digit)
elif (digit % 2)!=0:
digit = digit 2
new_num.append(digit)
elif (digit % 2)==0:
digit = digit-3
if digit>=0:
new_num.append(digit)
else:
digit = digit*(-1)
new_num.append(digit)
print(new_num)
# for single int instead of array
for digit in new_num:
digit = str(digit)
single_number = single_number digit
print(single_number)
new_number is array of digits single_number is the final number you want.
CodePudding user response:
is your code missing the indent after the for loop? I know that may not solve the question but is that another issue with your code or just a formatting issue here on stack?
num = int(input("Enter a number:"))
for digit in str(num):
print(digit)
if (num % 2)==0:
print(num 2)
else:
print(num - 3)
CodePudding user response:
num = int(input("Enter a number:"))
print(len(str(num)))
finaloutput = []
for i in range(len(str(num))):
digit = num
if digit%2==0:
if digit - 3 < 0:
digit = 0
else:
digit = digit - 3
else:
digit = digit 2
finaloutput.append(digit)
num = num //10
print(finaloutput)
string=""
for i in range(len(finaloutput)):
string = string str(finaloutput[i])
print(string[::-1])
Might be a big scuffed but gets the job done. Substract 3 from even, add 2 to odds, and watch for zeros.
output:
Enter a number:14502
5
[0, 0, 7, 1, 3]
31700
I put it so if an even number sub 3 is less than zero it jus stays zero, bc thats how I understood it. You can easily modify the code to suit your need and in accordance with your task or whatever
CodePudding user response:
Try:
num = 9876543210
l = [int(c) for c in str(s)]
l = [min(c, 7) 2 if c % 2 else max(c, 4) - 3 if c != 0 else c for c in l]
out = int(''.join(str(c) for c in l))
Output:
>>> out
9593715130
Details:
9 -> 9 (else 12)
8 -> 5 (-3)
7 -> 9 (else 10)
6 -> 3 (-3)
5 -> 7 ( 2)
4 -> 1 (-3)
3 -> 5 ( 2)
2 -> 1 (else -1)
1 -> 3 ( 2)
0 -> 0 (do nothing)
CodePudding user response:
A simple implementation based on your solution. It can only handle integers though. And there is one case which you have not specified which is
- what to do when the digit is 9? As
9 2 = 11
and 11 is not a digit but could well be what you want in your algorithm?
In this implementation 9 is turned to 1.
def calc_on_digits(num: str):
result: str = ""
for digit in num:
digit = int(digit)
if digit == 0:
result = "0"
continue
if digit % 2 == 0:
# in case it get negative take the absolute value
digit = abs(digit - 3)
else:
digit = 2
# in case its 9 the result would be 11 -> make it one
digit = digit % 10
result = str(digit)
return result
# string in this implementation can only contain valid integer values
print(calc_on_digits("14502"))
CodePudding user response:
You cannot calculate modulo from string. Also you have to perform the comparison per digit, not for the whole number.
num = input("Enter a number:") # no need to cast to int just to transform back to str below
new_number = ""
for digit in num:
digit = int(digit)
print(digit)
if (digit % 2) = 0:
tmp = digit 2
if tmp >= 10:
tmp = 0
else:
tmp = digit - 3
if tmp < 0:
tmp = 0
new_number = str(tmp)