Python convert email [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am writing a small program that reads in email and outputs only part of it.
Example
someone.lastname#example.com
I want the output to be someonel
so in this case i want the first part of the email before the "." and then the first letter of the second part in this case "l"
I need some assistance on how to concatenate these 2 parts taking only one character from the second part after the "."
thank you in advance

One way:
>>> first, rest = 'someone.lastname#example.com'.split('.', 1)
>>> first + rest[:1]
'someonel'

You should find the index of the first occurrence of dot and then use this index to split the string.
st = 'someone.lastname#example.com'
dot_index = st.index('.')
new_st = st.rjust(st[dot_index + 2])
This returns an array for new_st that its first element is what you want.
I used +2, because the argument is the length, so we should +1 the index and the second +1 is for counting the next character after the dot.

Related

Maximum Collatz length in Python, but output is not correct [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 11 months ago.
Improve this question
This is my first time posting to Stackoverflow.
I'm trying to solve this problem here: https://codingbat.com/prob/p270692?parent=/home/konstans#stuy.edu/all
When looking at all hailstone sequences from 1 to z, maxHail(z) will return the starting number that creates the longest sequence. In other words, maxHail(n) looks at hailLen(1), hailLen(2) ... hailLen(n) and returns the number from 1-n that had the largest hailstone sequence. You should look at the hailLen() problem before working on this. You should use your solution from the hailLen() problem. ( http://codingbat.com/author/p264289 ) since hailLen(3) is larger than the hailLen of 4 or 5, maxHail of 3,4,5 all return 3. Since 6 has a longer sequence, maxHail(6) gives us 6. remember: Use the hailLen function you already wrote!
Here's my code and the output:
However, I'm not sure where this goes wrong - I checked line-by-line and couldn't see anything wrong. Could anyone help me fix this? Thank you!
I see what is wrong - hailLen returns lenght of sequence and the question is about index for which the sequence is the longest. Just store it in variable
if (res := hailLen(i)) > counter: # it's python 3.8 syntax
counter = res
index = i
return index

Efficient way to generate all possibilities of string from characters [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 last year.
Improve this question
I am trying to randomly generate a string of n length from 5 characters ('ATGC '). I am currently using itertools.product, but it is incredibly slow. I switched to itertools.combinations_with_replacement, but it skips some values. Is there a faster way of doing this? For my application order does matter.
for error in itertools.product('ATGC ', repeat=len(errorPos)):
print(error)
for ps in error:
for pos in errorPos:
if ps == " ":
fseqL[pos] = ""
else:
fseqL[pos] = ps
If you just want a random single sequence:
import random
def generate_DNA(N):
possible_bases ='ACGT'
return ''.join(random.choice(possible_bases) for i in range(N))
one_hundred_bp_sequence = generate_DNA(100)
That was posted before post clarified spaces need; you can change possible_sequences to include a space if you need spaces allowed.
If you want all combinations that allow a space, too, a solution adapted from this answer, which I learned of from Biostars post 'all possible sequences from consensus':
from itertools import product
def all_possibilities_w_space(seq):
"""return list of all possible sequences given a completely ambiguous DNA input. Allow spaces"""
d = {"N":"ACGT "}
return list(map("".join, product(*map(d.get, seq))))
all_possibilities_w_space("N"*2) # example of length two
The idea being N can be any of "ACGT " and the multiple specifies the length. The map should specify C is used to make it faster according to the answer I adapted it from.

Assuming that s is a string of lower case characters. how would i find the number of string y inside s [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
the following problem in python please .....
Assuming that s is a string of lower case characters.
how would I write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then my program would print
'Number of times bob occurs is: 2'
I am a completely new to python and appreciate any help
If you didn't want to count overlapping bobs as separate values, this would be easy:
s.count('bob')
But you apparently do. (I had to guess that, based on the fact that your intended output is 2 rather than 1… in the future, it would be better to explain your problem instead of leaving it ambiguous.) As the help says, count returns "the number of non-overlapping occurrences of substring sub…", so that won't do any good.
So, for that, you will have to do it manually. I'll show an example that should have enough to get you started:
for i in range(len(s)):
if s[i:].startswith('bob'):
print('Found a bob')
A slightly smarter way to do this would be to use the find method on strings. You can find details on this in the online docs, or by typing help(str.find) in the interactive console. Notice that find takes a start argument. You should be able to figure out how this would help you; it may take a bit of work to get the details right, but if you get stuck, you can always post a new question asking for specific help.
You can try this way
string = "BOBOBOBOBOABCDE"
search = "BOB"
print len([i for i in range(len(string)) if string.startswith(search, i)])

How do i make Python count how many letters are in a word? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a lab for my programming class and i need to know how to make python count how many letters are in a word excluding spaces. Can anyone help me out? This is what i have so far.
def Na():
Name = raw_input("What is your name? : ")
count [char]
Na()
sum(c.isalpha() for c in Name)
Why not use len?
len([ltr for ltr in Name if ltr.isalpha()])
How about using string.ascii_letters:
len([c for c in Name if c in string.ascii_letters])
(see #abarnert's comment below about the option)
Or, may be use regular expressions:
len(re.findall('[a-zA-Z]', s))
You could try splitting the string by spaces, joining the newly formed list and taking the length of the resulting string like so:
len("".join(name.split(" ")))

What does line_spoken do in the program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can someone please explain this program, I don't understand where role, line-spoken come from
>>> data = open('sketch.txt')
>>> for each_line in data: // stores each line from the sketch file in each_line
... (role, line_spoken) = each_line.split(':')
... print(role, end='')
... print(' said: ', end='')
... print(line_spoken, end='')
You are looking at a tuple assignment.
The right-hand side expression is expected to have resulted in a sequence of two elements, and these two elements are assigned to the two named targets on the left hand side.
In other words, .split(:) is expected to return two values, and those two values are assigned to the variables role and line_spoken. Most likely, the lines in the file contain text like hamlet:To be or not to be, that is the question\n.
If each_line.split(':') does not return two values, an exception will be raised instead.
role and line_spoken are variables, which are populated with strings read from the file sketch.txt. sketch.txt contains colon-separated pairs of words or phrases, and role and line_spoken get those words/phrases.
The split() function returns a "tuple", which is "unpacked" into your two variables.
(Note that the parentheses around (role, line_spoken) are unnecessary.)

Categories