I was expecting this code to print "new string" between each row in my dataframe...
def isASCII(input_string):
print(input_string)
if isinstance(input_string, str):
orig_length = len(input_string)
print('new String!')
listThing = [letter for letter in input_string if ord(letter) < 127]
print(listThing)
new_length = len(listThing)
return (orig_length == new_length)
else:
return False
#isASCII('test') true
#isASCII('一些文字') false
#isASCII('sometext字') false
english_ga = dupe_free_ga.loc[isASCII(dupe_free_ga['App'].to_string())]
Instead 'new string!' appears once. Am I just not understanding how loc works here?
Let's try to split this line
english_ga = dupe_free_ga.loc[isASCII(dupe_free_ga['App'].to_string())]
to illustrate how Python evaluates it:
tmp = isASCII(dupe_free_ga['App'].to_string())
english_ga = dupe_free_ga.loc[tmp]
So, what would you expect dupe_free_ga.loc[True] or dupe_free_ga.loc[False] to return? Isn't that exactly what you get there?
Related
I wrote a regex code which compares two strings. It recognises a special character '?' that allows zero or more instances of previous character. It works fine until there are two or more occasions of '?' in the string. And I can't make out why.
def single_character_string(a, b) -> "return True if characters match":
"""check if two characters match"""
if len(a) == 0:
return True
elif len(b) == 0:
return False
else:
if a == '.':
return True
else:
if a == b:
return True
else:
return False
def meta_question_result(temp):
if len(temp) >= 2:
if temp[1] == '?':
k_1 = temp.replace(temp[0: 2], '') # no char
k_2 = temp.replace(temp[1], '') # char
return k_1, k_2
def check_pair_by_pair(template, check_string) -> "Strings are of Equal length! " \
"return True if lines are identical":
"""check if two strings match symbol by symbol. template may be less than string, the opposite
is False"""
if not template: # exit from recursion
return True
if not check_string: # exit from recursion
return False
if meta_question_result(template):
t_1, t_2 = meta_question_result(template)
if single_character_string(t_1[0], check_string[0]):
return check_pair_by_pair(t_1[1:], check_string[1:])
if single_character_string(t_2[0], check_string[0]):
return check_pair_by_pair(t_2[1:], check_string[1:])
else:
return False
elif single_character_string(template[0], check_string[0]):
return check_pair_by_pair(template[1:], check_string[1:])
else:
return False
reg, st = input().split("|")
print(check_pair_by_pair(reg, st))
reg = "co?lou?r"
st = "colour"
gives True as expected,
reg = "co?lou?r"
st = "clor"
gives True as expected,
but...
reg = "co?lou?r"
st = "color"
gives False. I expected True.
Found the bag.
Replace method replaces all instances of '?'. So the second '?' was replaced also and program didn't see it.
I should add an argument 'count' to replace method that is equal to 1.
k_1 = temp.replace(temp[0: 2], '', 1) # no char
I am trying to build a Pig Latin translator. I want to remove all the characters on the start of a string and store them in a variable. I have already done it to the end, but for some reason when I try and do it the the start it messes up the word.
For example, when I input *test* it returns *stteay* when it should return *esttay*. Also if you put in test* it works fine. I have no clue why.
The funny thing is, that if I only put a special character to the end of the string it works fine! The two pieces of code for extracting the special chars are pretty much identical as well.
Here is the code:
def special_chars_check(text: str) -> bool:
return any(c for c in text if not c.isalnum() and not c.isspace())
def convert(stuff):
words = stuff.split()
output = ''
for i in words:
post_special_chars = ''
reversed_temp = i[::-1]
for c in reversed_temp:
if special_chars_check(reversed_temp[0]):
if special_chars_check(c):
post_special_chars = f'{post_special_chars}{reversed_temp[0]}'
reversed_temp = reversed_temp[1:]
i_temp = reversed_temp[::-1]
else:
break
if len(post_special_chars) == 0:
i_temp = i
pre_special_chars = ''
normal_temp = i_temp
for c in normal_temp:
if special_chars_check(normal_temp[0]):
if special_chars_check(c):
pre_special_chars = f'{pre_special_chars}{normal_temp[0]}'
normal_temp = normal_temp[1:]
i_temp = normal_temp
else:
break
if len(pre_special_chars) == 0:
i_temp = i
allcaps = False
firstcaps = False
if i.isupper():
allcaps = True
elif i[0].isupper():
firstcaps = True
i_temp2 = i_temp
vowel_in = False
for c in i:
if c in 'aeiouAEIOU':
vowel_in = True
if i[0] in 'aeiouAEIOU' and vowel_in:
if allcaps:
output = f'{output} {pre_special_chars}{i_temp.upper()}WAY{post_special_chars}'
elif firstcaps:
temp_output = i.lower()
temp_char2 = temp_output[0]
temp_output = temp_output[1:]
final_i_temp = f'{temp_char2.upper()}{temp_output}'
output = f'{output} {pre_special_chars}{final_i_temp}way{post_special_chars}'
else:
output = f'{output} {pre_special_chars}{i_temp}way{post_special_chars}'
elif i[0] not in 'aeiouAEIOU' and vowel_in:
fulfilled = False
for c in i:
if not fulfilled:
if c in 'aeiouyAEIOUY':
if allcaps:
output = f'{output} {pre_special_chars}{i_temp.upper()}AY{post_special_chars}'
elif firstcaps:
temp_output = i_temp.lower()
temp_char2 = temp_output[0]
temp_output = temp_output[1:]
final_i_temp = f'{temp_char2.upper()}{temp_output}'
output = f'{output} {pre_special_chars}{final_i_temp}ay{post_special_chars}'
else:
output = f'{output} {pre_special_chars}{i_temp}ay{post_special_chars}'
fulfilled = True
else:
temp_char = i_temp2[0]
i_temp = f'{i_temp2[1:]}{temp_char}'
i_temp2 = i_temp
fulfilled = False
else:
output = f'{output} {i}'
try:
output[1:]
except:
return output
else:
return output[1:]
while True:
print(convert(input('Enter text: ')))
It would really help if someone could point out my mistake.
Thankyou!
I just found the answer... It was a silly little mistake. On line 63 for c in i: needed to be for c in i_temp: Thankyou to #JustLearning for the advice.
Looking for some help on a tick tac toe exercise:
I have the bellow:
test_board = ['#','X','O','X','O','X','O','','O','X']
def space_check(board,position):
free_space = ""
if free_space in board[position]:
return True
else:
return False
When running the function I cannot see the False return, only True:
space_check(test_board,7)
Output: True
space_check(test_board,9)
Output: True
If free_space in board[position]
You are search a "" in a string not in a list of string,so the results always true because "" always exist in any string.
You just Use equal operation == instead of in.
Change your function with this:
def space_check(board,position):
free_space = ""
return free_space == board[position]
test_board = ['#','X','O','X','O','X','O','','O','X']
space_check(test_board, 7)
Output : True
space_check(test_board, 9)
Output: False
I'm trying to write a function in similar manner as started, so that I will get what it's doing. I'm assuming this can be done with one line of code, with some fancy functions, but for the sake of practice and understanding I'm trying to come up with similar solution.
The task is the following: the function takes a text once it encounters enclosed square brackets [ word ] It should print out or return all words which are between square brackets. For example, if the text string would be "[a]n example[ string]", you are expected to print out "a string".
def string():
text = "some random text [and I need this bit of txt] but I don't know how to continue [to get this bit as well]"
for i in text:
for j in range(len(text)):
if text[j] == '[':
new = text.find(']')
return(text[j+1:new])
print(string())
Try this:
def extract(text, skip_chars=("\n", )):
output = ""
flag = False
for c in text:
if c == "]":
flag = False
if flag and not c in skip_chars:
output += c
if c == "[":
flag = True
return output
print(extract("""[a]n example[
stri
ng]"""))
# -> "a string"
def string():
result = []
text = "some random text [and I need this bit of txt] but I don't know how to continue [to get this bit as well]"
for i in text:
if i == '[':
new = text.find(']')
result.append(text[text.index(i) + 1:new])
return " ".join(result)
print(string())
def parse(source):
i = source.index("[") # throw an exception
result = ""
while i < len(source):
if s[i] == "[":
i += 1
while i < len(source):
temp = ""
if source[i] == "]":
result += temp
break;
temp += source[i]
i += 1
i += 1
return result
I have this code that should break once it fulfills a certain condition, ie when the isuniquestring(listofchar) function returns True, but sometimes it doesn't do that and it goes into an infinite loop. I tried printing the condition to check if there's something wrong with the condition, but even when the condition is printed true the function still continues running, I have no idea why.
one of the strings that throws up an infinite loop is 'thisisazoothisisapanda', so when I do getunrepeatedlist('thisisazoothisisapanda'), it goes into an infinite loop.
would be really grateful if someone could help
thanks!
Here's my code:
def getunrepeatedlist(listofchar):
for ch in listofchar:
if isrepeatedcharacter(ch,listofchar):
listofindex = checkrepeatedcharacters(ch,listofchar)
listofchar = stripclosertoends(listofindex,listofchar)
print (listofchar)
print (isuniquestring(listofchar))
if isuniquestring(listofchar):
return listofchar
#print (listofchar)
else:
getunrepeatedlist(listofchar)
return listofchar
just for reference, these are the functions I called
def isrepeatedcharacter(ch,list):
if list.count(ch) == 1 or list.count(ch) == 0:
return False
else:
return True
def checkrepeatedcharacters(ch,list):
listofindex=[]
for indexofchar in range(len(list)):
if list[indexofchar] == ch:
listofindex.append(indexofchar)
return listofindex
def stripclosertoends(listofindices,listofchar):
stringlength = len(listofchar)-1
if listofindices[0] > (stringlength-listofindices[-1]):
newstring = listofchar[:listofindices[-1]]
elif listofindices[0] < (stringlength-listofindices[-1]):
newstring = listofchar[listofindices[0]+1:]
elif listofindices[0] == (stringlength-listofindices[-1]):
beginningcount = 0
endcount = 0
for index in range(listofindices[0]):
if isrepeatedcharacter(listofchar[index],listofchar):
beginningcount += 1
for index in range(listofindices[-1]+1,len(listofchar)):
if isrepeatedcharacter(listofchar[index],listofchar):
endcount += 1
if beginningcount < endcount:
newstring = listofchar[:listofindices[-1]]
else:
#print (listofindices[0])
newstring = listofchar[listofindices[0]+1:]
#print (newstring)
return newstring
def isuniquestring(list):
if len(list) == len(set(list)):
return True
else:
return False
It may be due to the fact that you are changing listofchar in your for loop. Try cloning that variable to a new name and use that variable for manipulations and return the new variable.