how can I solve this in python? [closed] - python

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

Related

Math program Python [closed]

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 9 months ago.
Improve this question
I'm struggling to implement a program that prompts the user for an arithmetic expression,
(for example; 1 + 1) and then calculates and outputs the result as a floating-point value formatted to one decimal place (for example; 2.0).
Also, I want there to be a space between x and y, and a space between y and z in the equation (for example; 1 + 1, with spaces between each character).
This is basically my desired output (in a terminal):
https://i.stack.imgur.com/5LuRH.png
I'm pretty new to python so this will probably seem easier to more experienced coders, but anyone have any ideas, and thanks in advance.
You can just simply use eval directly on the expression inserted...
expression = str(input('Expression: '))
print("{:.1f}".format(eval(expression), 1))
output:
Expression: 1 + 1
2.0

Replace word by word in a string [closed]

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.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
I want to replace a word in a string which is determined by the word entered by the user.
For example, if user enters "pot", the code replaces it with "who" but if he enters "top" then it becomes "mam".
I have a set of 225 words and their replacements. I am using python for this. Replacing each word individually in a condition is really impractical. is their a quicker way to do this. I currently have no code as I am totally confused how to do this.
Using excel throws an error no matter what.
Yes, there is a very convenient way to do this; python dicts:
d = {"pot": "who", "top": "mam"}
print(d["pot"])
print(d["top"])
Output:
who
mam
In Python 3.1, you can use the new match - case statement:
s = "pot"
match s:
case "pot":
print("who")
case "top":
print("top")
case _:
print(None)

Destroy 2 caracters after a number [closed]

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).

Analyzing strings in Python [closed]

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 4 years ago.
Improve this question
I'd like to make a program in python that analyzes a users input.
For example if the user writes "Hello, I'm 19 years old", the program can see that
the string has numbers in it and assigns "19" to a variable.
It could also at the same time see that the string has the word "years" in it and makes the variable:
years = 19
I'm pretty new to python and programming in general, so sorry if the question is weird.
Thanks.
You can count the length of the string using function len. str can contain all chars and letters. Try the following code:
word = input("Waiting for input: ")
print("Found "+str(len(word))+" letters!")
Testing it:
Waiting for input: hello
Found 5 letters!
Testing it with numbers and other chars:
Waiting for input: hello123!##
Found 11 letters!

Check whether text contains x numbers in a row [closed]

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 7 years ago.
Improve this question
Is there an option to check whether the given text contains x numbers in a row?
Example:
da$ c0220 -> True
dsad458d69 -> False
I think I should use regular expressions but I can't figure out how.
The regex below checks for 10 consecutive numbers
\d{10}
In Python this becomes
if re.search(r"\d{10}", subject):
# Successful match
else:
# Match attempt failed

Categories

Resources