I know how to remove the first occurrence of a letter using splicing, but I'm trying to achieve this without using any of the string functions like splicing, .find(), .count(), etc. I can't seem to figure out how you complete it without splicing.
Here's what I currently have with splicing that works correctly:
s1 = ''
s2 = len(remWord)
for i in range(s2):
if (remWord[i] == remLetter):
s1 = remWord[0:i] + remWord[i + 1:s2]
return s1
Any assistance would be great.
I am assuming that you do not remove anything if the letter is not contained in the string, so you can use the following code:
Strictly not using splicing
new_string = ''
found = False
for i in range(len(remWord)):
if remWord[i] != remLetter or found:
new_string += remWord[i]
else:
found = True
If you are allowed to use splicing
new_string = ''
for i in range(len(remWord)):
if remWord[i] != remLetter:
new_string += remWord[i]
else:
break
new_string += remWord[i + 1:]
You should stop the processing as soon as you encounter the first letter, use a break for this purpose.
remWord = 'this is the word to process'
remLetter = 's'
s1 = ''
s2 = len(remWord)
for i in range(s2):
if (remWord[i] == remLetter):
s1 = remWord[0:i] + remWord[i + 1:s2]
break
print s1
output:
$ python firstletter.py
thi is the word to process
If you omit the break, your output will be like this
$ python firstletter.py
this is the word to proces
As the if clause will be satisfied for the last encounter of the remLetter present in your input string
Strings are sequence types, so we can do this (mostly) with just sequence operations.
seen = False
chars = ''
for ch in remWord:
if ch == remLetter and not seen:
seen = True
else:
chars += ch
The trouble with this is it’s a little expensive because you’re creating a new string each time. Alternatively you can make chars a list and use StringIO, or ''.join if you can get away with it.
Related
So let's say I have a string, like this:
string = '12345+67890'
I want to go back to search for that plus, and add a letter 'H', so the end result would be like this:
string = '12345+h67890
Also, what if I want to get the latest +, so if there is two +'s, I want to get the last +?
Any help would be appreciated!
You could use reverse split to split on the last instance of +, then join with +H:
>>> '+H'.join('123+456+789'.rsplit('+',1))
'123+456+H789'
Convert into list, iterate through list, when you find the plus add an 'h' into the next index.
string = '12345+67890'
stringList = list(string)
i = 0
newString = ''
for i in range(len(stringList)):
if stringList[i] == '+':
stringList.insert(i+1,'h')
for letter in stringList:
newString += letter
print(newString)
Since you asked how to do it with if statements:
i = -1
for i in range(len(my_str)):
if my_str[i] == '+':
plus = i+1 # <- will update every time you see a + symbol
if i != -1:
my_str = my_str[:i] + 'h' + my_str[i:]
Alternatively, you can search backwards:
i = -1
for i in reversed(range(len(my_str))):
if my_str[i] == '+':
plus = i+1 # <- will update every time you see a + symbol
break
if i != -1:
my_str = my_str[:i] + 'h' + my_str[i:]
As others have mentioned you can use built-in functions like find and rfind. My personal choice is to refer to regular expressions for this type of thing:
import re
my_str = re.sub('(.*\+)(.*)',r"\1h\2", my_str))
I'd like write code to find specific instances of words in a long string of text, where the letters making up the word are not adjacent, but consecutive.
The string I use will be thousands of characters long, but a as a shorter example... If I want to find instances of the word "chair" within the following string, where each letter is no more than 10 characters from the previous.
djecskjwidhl;asdjakimcoperkldrlkadkj
To avoid the problem of finding many instances in a large string, I'd prefer to limit the distance between every two letters to 10. So the word chair in the string abcCabcabcHabcAabdIabcR would count. But the word chair in the string abcCabcabcabcabcabcabcabcabHjdkeAlcndInadhR would not count.
Can I do this with python code? If so I'd appreciate an example that I could work with.
Maybe paste the string of text or use an input file? Have it search for the word or words I want, and then identify if those words are there?
Thanks.
This code below will do what you want:
will_find = "aaaaaaaaaaaaaaaaaaaaaaaabcCabcabcHabcAabdIabcR"
wont_find = "abcCabcabcabcabcabcabcabcabHjdkeAlcndInadhR"
looking_for = "CHAIR"
max_look = 10
def find_word(characters, word):
i = characters.find(word[0])
if i == -1:
print("I couldnt find the first character ...")
return False
for symbol in word:
print(characters[i:i + max_look+1])
if symbol in characters[i:i + max_look+1]:
i += characters[i: i + max_look+1].find(symbol)
print("{} is in the range of {} [{}]".format(symbol, characters[i:i+ max_look], i))
continue
else:
print("Couldnt find {} in {}".format(symbol, characters[i: i + max_look]))
return False
return True
find_word(will_find, looking_for)
print("--------")
find_word(wont_find, looking_for)
An alternative, this may also work for you.
long_string = 'djecskjwidhl;asdjakimcoperkldrlkadkj'
check_word = 'chair'
def substringChecker(longString, substring):
starting_index = []
n , derived_word = 0, substring[0]
for i, char in enumerate(longString[:-11]):
if char == substring[n] and substring[n + 1] in longString[i : i + 11]:
n += 1
derived_word += substring[n]
starting_index.append(i)
if len(derived_word) == len(substring):
return derived_word == substring, starting_index[0]
return False
print(substringChecker(long_string, check_word))
(True, 3)
To check if the word is there:
string = "abccabcabchabcaabdiabcr"
word = "chair"
while string or word:
index = string[:10].find(word[0])
if index > -1:
string = string[index+1:]
word = word[1:]
continue
if not word:
print("found")
else:
break
I need to find the first vowel of a string in python, and I'm a beginner. I'm instructed to move the characters before the first vowel to the end of the word and add '-ay'. For example "big" becomes "ig-bay" and "string" becomes "ing-stray" (piglatin, basically).
This is what I have so far:
def convert(s):
ssplit = s.split()
beginning = ""
for char in ssplit:
if char in ('a','e','i','o','u'):
end = ssplit[char:]
strend = str(end)
else:
beginning = beginning + char
return strend + "-" + beginning + "ay"
I need to find a way to stop the "if" statement from looking for further vowels after finding the first vowel - at least I think it's the problem. Thanks!
Break things down one step at a time.
Your first task is to find the first vowel. Let's do that:
def first_vowel(s):
for index, char in enumerate(s):
if char in 'aeiou':
return index
raise Error('No vowel found')
Then you need to use that first vowel to split your word:
def convert(s):
index = first_vowel(s)
return s[index:] + "-" + s[:index] + 'ay'
Then test it:
print(convert('pig'))
print(convert('string'))
Full code, runnable, is here: https://repl.it/Dijj
The exception handling, for words that have no vowels, is left as an exercise.
Add a break where you want the for loop to end: https://docs.python.org/2/tutorial/controlflow.html
Python has the break and continuestatements for loop control. You can set a boolean that you trigger such that:
if flag:
break
#do code
#set flag
You can use a break statement as soon as you found a vowel.
You also do not need to use any split() functions.
One big mistake you did was using char to get the SubString. You need to use the index of that char to get the SubString instead.
Take a look at this:
def convert(s):
beginning = ""
index = 0;
for char in s:
if char in ('a','e','i','o','u'):
end = str(s[index:])
break
else:
beginning = beginning + char
index = index + 1
return str(end) + "-" + beginning + "ay"
Side note. You can use a regex:
>>> import re
>>> cases=['big','string']
>>> for case in cases:
... print case+'=>', re.sub(r'^([^aeiou]*)(\w*)', '\\2-\\1ay', case)
...
big=> ig-bay
string=> ing-stray
If I have a string
String = 'ABCEEFGH'
How can I check what letter is beside each letter without going out of index?
for index in range(len(String)):
if String[index] == String[index+1]:
print('Double')
You can use enumerate, slicing the string up to the second last character:
String = 'ABCEEFGH'
for ind,ch in enumerate(String[:-1]):
if ch == String[ind+1]:
print('Double')
In your own code the logic would be the same len(String)-1 but enumerate is the way to go:
for index in range(len(String)-1):
if String[index] == String[index+1]:
print('Double')
The fact you seen to only want to check if any two adjacent characters are identical, maybe using any would be best:
String = 'ABCEEFGH'
if any( ch == String[ind+1] for ind, ch in enumerate(String[:-1])):
print('Double',ch)
any will short circuit and break the loop as soon the condition is Trueor else evaluate to False if we have no match.
>>> text = 'ABCEEFGH'
>>> for c1, c2 in zip(text, text[1:]):
if c1 == c2:
print 'double'
double
These kinds of problems are almost always easier if you think of comparing with the previous letter instead of the next one. It's a lot easier to remember letters you've already seen than to look ahead.
text = 'ABCEEFGH'
prev = ''
for letter in text:
if letter == prev:
print("letter duplicated:", letter)
prev = letter
You can use regular expressions:
for match in re.findall(r'([a-z])\1', your_string):
print('Double letters found here.')
I've been working on this Palindrome program and am really close to completing it.Close to the point that it's driving me a bit crazy haha.
The program is supposed to check each 'phrase' to determine if it is a Palindrome or not and return a lowercase version with white space and punctuation removed if it is in fact a Palindrome. Otherwise, if not, it's supposed to return None.
I'm just having an issue with bringing my test data into the function. I can't seem to think of the correct way of dealing with it. It's probably pretty simple...Any ideas?
Thanks!
import string
def reverse(word):
newword = ''
letterflag = -1
for numoletter in word:
newword += word[letterflag]
letterflag -= 1
return newword
def Palindromize(phrase):
for punct in string.punctuation:
phrase= phrase.replace(punct,'')
phrase = str(phrase.lower())
firstindex = 0
secondindex = len(phrase) - 1
flag = 0
while firstindex != secondindex and firstindex < secondindex:
char1 = phrase[firstindex]
char2 = phrase[secondindex]
if char1 == char2:
flag += 1
else:
break
firstindex += 1
secondindex -= 1
if flag == len(phrase) // (2):
print phrase.strip()
else:
print None
def Main():
data = ['Murder for a jar of red rum',12321, 'nope', 'abcbA', 3443, 'what',
'Never odd or even', 'Rats live on no evil star']
for word in data:
word == word.split()
Palindromize(word)
if __name__ == '__main__':
Main()
Maybe this line is causing the problems.
for word in data:
word == word.split() # This line.
Palindromize(word)
You're testing for equality here, rather than reassigning the variable word which can be done using word = word.split(). word then becomes a list, and you might want to iterate over the list using
for elem in word:
Palindromize(elem)
Also, you seem to be calling the split method on int, which is not possible, try converting them to strings.
Also, why do you convert the phrase to lower case in the for loop, just doing it once will suffice.
At the "core" of your program, you could do much better in Python, using filter for example. Here is a quick demonstration:
>>> phrase = 'Murder for a jar of red rum!'
>>> normalized = filter(str.isalnum, phrase.lower())
>>> normalized
'murderforajarofredrum'
>>> reversed = normalized[-1::-1]
>>> reversed
'murderforajarofredrum'
# Test is it is a palindrome
>>> reversed == normalized
True
Before you go bananas, let's rethink the problem:
You have already pointed out that Palindromes only make sense in strings without punctuation, whitespace, or mixed case. Thus, you need to convert your input string, either by removing the unwanted characters or by picking the allowed ones. For the latter, one can imagine:
import string
clean_data = [ch for ch in original_data if ch in string.ascii_letters]
clean_data = ''.join(clean_data).lower()
Having the cleaned version of the input, one might consider the third parameter in slicing of strings, particularly when it's -1 ;)
Does a comparison like
if clean_data[::-1] == clean_data:
....
ring a bell?
One of the primary errors that i spotted is here:
for word in data:
word==word.split()
Here, there are two mistakes:
1. Double equals make no point here.
2. If you wish to split the contents of each iteration of data, then doing like this doesn't change the original list, since you are modifying the duplicate set called word. To achieve your list, do:
for i in range(data):
data[i]=data[i].split()
This may clear your errors