Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to add the integers from double digit number(triple etc.) together
Block quote 22 = 2+2 = 4
I considered doing it manually but because I have 999 number to get through that'll take a really long time.
I tried making two separate lists and adding it together but continually received syntax errors.
*edited for extra detail *
You can convert the integer to a string, and then sum the values up:
count = sum((int(digit) for digit in str(22)))
print(count)
This iterates over every digit, converts it back to an integer, and finally computes the sum over all digits.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How can I separate data types in a string or list so they can be set to another character, I assume there is something I have missed but everything i have tried so far has now worked for me. so far have tried so split into list and use a for loop to find every int but I can't find a way to differentiate the data types so it can change every int.
You can use regex :
import re
re.sub("\d", "_", "10 4 2")
\d matches any decimal digit character.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I need help, in a long string, I want to know the the 2 characters after a number
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
if in example after 1 number == "gr":
so delete "gr"
If I understand your question correctly, what you need is regex:
import re
example = "gz15gr123da1az4gr1sd23f168zgre4r6z"
re.sub("(\d)gr", r"\1", example)
Output
gz15123da1az41sd23f168zgre4r6z
Explnatation
The re.sub function takes three arguments: the first argument is a patter, the second one the replacement, and the last of is the string itself. (\d)gr selects the parts that contain a number followed by gr, then replaces it with the number (\d) itself(removing the gr part).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Id codes at a company come in the form x-y-zzzzzz, where x is a digit and y is a letter and zzzzzz represents a string of 6 letters. Write a function which takes in a code as an input (e.g. 3-a-abaabb) and returns the zzzzzz part (e.g. abaabb).
I have no idea how to start and solve this question. any help would be much appreciated. My IDE is pycharm (solving python coding problems) I basically need to create a function which takes the code as an input and will return the last 6 letters
You can use str.spit('-') then search count in code with repeat is equal or not, like below:
def fnd_code(code):
repeat, char, search = code.split('-')
return search.count(char) == int(repeat)
print(fnd_code('3-a-abaabb'))
print(fnd_code('4-a-abaabb'))
Output:
True
False
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I expected result is sometimes getting padded with 000 or 0000 or 0
Expected : 2
Actual : 0002 or 02
The comparison is failing where it should pass.
If you're dealing with numbers, use Should Be Equal As X, where X is either Numbers if they are floats, or Integers - if whole numbers. The keyword will cast the compared values, and do comparison where the padding doesn't matter:
Should Be Equal As Numbers 002.9080 000002.908 # will pass
If any of the arguments cannot be casted - they have a letter inside them, for example, the keyword will fail.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sample Input (in Plaintext)
1
1 2
Description- The first line contains an integer t, denoting the number of test cases. Next t lines contain two integers, a and b separated by a space.
My question is how can I load these inputs into variables using Python 2.7? Also I need to load this into IDE without using "xxx.py > input.txt"
raw_input() takes in a line of input as a string. The .split() string method produces a list of the whitespace-separated words in a string. int() parses a string as an int. So putting that together, you can read the input you want using
t = int(raw_input())
cases = []
for i in range(t):
cases.append( map(int, raw_input().split()) )
This will give you a list of a,b pairs if the input is in the correct format, but won't do any error checking.