Scrabble Score Count - python

I have been trying to make a scrabble score counter but I end up falling in an infinite loop, I attach my code in case anyone can give me a hand, it personally makes sense, but for some reason Python does not like my code :((
def letterScore(let):
"""Argument let: one character string
Return Value: the scrabble value of the letter"""
if let in 'qz':
return 10
elif let in 'aelnorstu':
return 1
elif let in 'd':
return 2
elif let in 'bcmp':
return 3
elif let in 'vwy':
return 4
elif let in 'k':
return 5
elif let in 'x':
return 8
else:
return 0
def scrabbleScore (S):
""""Argument S: String argument S
Return Value: the total Scrabble score :))"""
if "s[0]" == '':
return 0
else:
return letterScore(S[0]) + scrabbleScore (S[1:])
#
# Tests
#
print( "scrabbleScore('quetzal') should be 25 :", scrabbleScore('quetzal') )

As the comments suggested, you should check for the string to be empty with S == ''. The string comparison you had before made no sense, as it was always false.
def scrabbleScore(S):
""""Argument S: String argument S
Return Value: the total Scrabble score :))"""
if S == '':
return 0
else:
return letterScore(S[0]) + scrabbleScore(S[1:])

Related

simple string program doesent work, python

A friend of mine told me that she needs help with some homework, I owe her a favor so I said fine, why not. she needed help with a program that checks a sequence, if the sequence is made of the same 2 chars one after the other it will print "yes" (for example "ABABABAB" or "3$3$3$3:)
The program works fine with even length strings (for example "abab") but not with odd length one ("ububu")
I made the code messy and "bad" in purpose, computers is her worst subject so I don't want it to look obvious that someone else wrote the code
the code -
def main():
StringInput = input('your string here - ')
GoodOrBad = True
L1 = StringInput[0]
L2 = StringInput[1]
i = 0
while i <= len(StringInput):
if i % 2 == 0:
if StringInput[i] == L1:
i = i + 1
else:
GoodOrBad = False
break
if i % 2 != 0:
if StringInput[i] == L2:
i = i + 1
else:
GoodOrBad = False
break
if GoodOrBad == True:
print("yes")
elif GoodOrBad != True:
print("no")
main()
I hope someone will spot the problem, thanks you if you read everything :)
How about (assuming s is your string):
len(set(s[::2]))==1 & len(set(s[1::2]))==1
It checks that there is 1 char in the even locations, and 1 char in the odd locations.
a) Showing your friend bad and messy code makes her hardly a better programmer. I suggest that you explain to her in a way that she can improve her programming skills.
b) If you check for the character at the even position and find that it is good, you increment i. After that, you check if i is odd (which it is, since you found a valid character at the even position), you check if the character is valid. Instead of checking for odd position, an else should do the trick.
You can do this using two methods->
O(n)-
def main():
StringInput = input('your string here - ')
GoodOrBad = True
L1 = StringInput[0]
L2 = StringInput[1]
i = 2
while i < len(StringInput):
l=StringInput[i]
if(l==StringInput[i-2]):
GoodOrBad=True
else:
GoodOrBad=False
i+=1
if GoodOrBad == True:
print("yes")
elif GoodOrBad == False:
print("no")
main()
Another method->
O(1)-
def main():
StringInput = input('your string here - ')
GoodOrBad = True
L1 = set(StringInput[0::2])
L2 = set(StringInput[1::2])
if len(L1)==len(L2):
print("yes")
else:
print("no")
main()
There is a lot in this that I would change, but I am just showing the minimal changes to get it to work. There are 2 issues.
You have an off by one error in the code:
i = 0
while i <= len(StringInput):
# in the loop you index into StringInput
StringInput[i]
Say you have 5 characters in StringInput. Because your while loop is going from i = 0 to i < = len(StringInput), it is going to go through the values [0, 1, 2, 3, 4, 5]. That last index is a problem since it is off the end off StringInput.
It will throw a 'string index out of range' exception.
You need to use:
while i < len(StringInput)
You also need to change the second if to an elif (actually it could just be an else, but...) so you do not try to test both in the same pass of the loop. If you go into the second if after the last char has been tested in the first if it will go out of range again.
elif i % 2 != 0:
So the corrected code would be:
def main():
StringInput = input('your string here - ')
GoodOrBad = True
L1 = StringInput[0]
L2 = StringInput[1]
i = 0
while i < len(StringInput):
if i % 2 == 0:
if StringInput[i] == L1:
i = i + 1
else:
GoodOrBad = False
break
elif i % 2 != 0:
if StringInput[i] == L2:
i = i + 1
else:
GoodOrBad = False
break
if GoodOrBad == True:
print("yes")
elif GoodOrBad != True:
print("no")
main()
def main():
StringInput = input('your string here - ')
MaxLength = len(StringInput) // 2 + (len(StringInput) % 2 > 0)
start = StringInput[:2]
chained = start * MaxLength
GoodOrBad = chained[:len(StringInput)] == StringInput
if GoodOrBad == True:
print("yes")
elif GoodOrBad != True:
print("no")
I believe this does what you want. You can make it messier if this isn't bad enough.

function either returns None or says "maximum recursion depth exceeded"

I am trying to create a program that will take two sets of random integers, and print a statement based on the results of those two sets of integers. However, when I call the method, I either receive "None" or an error stating "maximum recursion depth exceeded". I can't seem to figure out how to structure my return statements within these methods so that this works properly.
def genre(a,b):
genreType = random.randint(a,b)
if genreType == '1':
genreType = "Fantasy"
return genre()
elif genreType == '2':
genreType = "Sci-Fi"
return genre()
def medium():
mediumType = random.randint(1,2)
if mediumType == '1':
genre = genre(1,2)
print("Play a " + genre + "game")
return medium()
elif mediumType == '2':
genre = genre(1,2)
print("Watch a " + genre + "anime")
return medium()
First, if a function has a branch without a return, it will return None, e.g.:
def something():
if False:
return "Thing"
# There is no return in "else"
print(something()) # None
Second, comparing numbers to strings never succeeds:
print(1 == 1) # True
print(1 == '1') # False
So the example you've provided can only always return None
Third, you are not returning anything meaningful from your functions:
def genre(a,b):
genreType = random.randint(a,b)
if genreType == '1':
genreType = "Fantasy"
return genre() # call this function again, but with no parameters, why?!
If the condition had a chance of being true, you would be getting
TypeError: genre() missing 2 required positional arguments: 'a' and 'b'
I can only guess that you meant to do this:
if genreType == 1:
genreType = "Fantasy"
return genreType
Or, shorter and arguably more readable:
def genre(a,b):
genreType = random.randint(a,b)
if genreType == 1:
return "Fantasy"
elif genreType == 2:
return "Sci-Fi"
# And you can add your own error to know what exactly went wrong
else:
raise Exception("Genre bounds must be between 1 and 2")

Getting "string index out of range" error

The code is from CodingBat and the question is: Return True if the string "cat" and "dog" appear the same number of times in the given string.
I have the solution but it states that the string index is out of range. I do not really understand what this means since I am new to python.
def cat_dog(str):
dogCount = 0
catCount = 0
length = len(str)
if length > 6:
for i in range(length-1):
if str[i]== 'd' and str[i+1]== 'o' and str[i+2]== 'g':
dogCount +=1
elif str[i]== 'c' and str[i+1]== 'a' and str[i+2]== 't':
catCount +=1
else:
return False
if dogCount == catCount:
return True
else:
return False
Expect either a true or false based on the function results
String index out of range error means you're trying to access a letter beyond the length of the string. This is happening in your for loop because when i = length - 2, str[i+2] becomes str[length] which is beyond the length of the string. Also, you should change if length > 6 to if length >= 6. Try this code:
def cat_dog(string):
dogCount = 0
catCount = 0
length = len(string)
if length >= 6:
for i in range(length-2):
if string[i]== 'd' and string[i+1]== 'o' and string[i+2]== 'g':
dogCount +=1
elif string[i]== 'c' and string[i+1]== 'a' and string[i+2]== 't':
catCount +=1
else:
return False
if dogCount == catCount:
return True
else:
return False
A possible solution is to continuously check the string to see if it contains either of the words replacing the first occurrence until there is no more:
def cat_dog(string):
dogCount = 0
catCount = 0
while "dog" in string:
dogCount += 1
string = string.replace("dog", "", 1)
while "cat" in string:
catCount += 1
string = string.replace("cat", "", 1)
if dogCount == catCount:
return True
else:
return False

Python brute force password guesser

I am doing a task in class about a password guesser. I stumbled into a lot of problems trying to solve this task, my first approach was to use for loops (code below), but I realized that the amount of 'for loops' is equal to the length of the string.
a_z = 'abcdefghijklmnopqrstuvwxyz'
pasw = 'dog'
tests = 0
guess = ''
azlen = len(a_z)
for i in range(azlen):
for j in range(azlen):
for k in range(azlen):
guess = a_z[i] + a_z[j] + a_z[k]
tests += 1
if guess == pasw:
print('Got "{}" after {} tests'.format(guess, str(tests)))
break
input()
The program above is very concrete. It only works if there are exactly 3 characters entered. I read that you could use a package called intertools, however, I really want to find another way of doing this. I thought about using recursion but don't even know where to start.
import string
import itertools
for possible_password in itertools.permutations(string.ascii_letters, 3):
print(possible_password)
If you don't want to use itertools you can certainly do this with recursion, which will work with passwords of any (reasonable) length—it's not wired to three characters. Basically, each recursive call will attempt to append a new character from your alphabet to your running value of guess. The base case is when the guess attains the same length as value you're seeking, in which case you check for a match. If a match is found, return an indication that you have succeeded (I used return True) so you can short circuit any further searching. Otherwise, return a failure indication (return False). The use of a global counter makes it a bit uglier, but produces the same results you reported.
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def brute_force_guesser(passwd, guess = ''):
global _bfg_counter
if len(guess) == 0:
_bfg_counter = 0
if len(guess) == len(passwd):
_bfg_counter += 1
if guess == passwd:
print('Got "{}" after {} tests'.format(guess, str(_bfg_counter)))
return True
return False
else:
for c in ALPHABET:
if brute_force_guesser(passwd, guess + c):
return True
return False
brute_force_guesser('dog') # => Got "dog" after 2399 tests
brute_force_guesser('doggy') # => Got "doggy" after 1621229 tests
One way to avoid the global counter is by using multiple return values:
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
def brute_force_guesser(target, guess = '', counter = 0):
if len(guess) == len(target):
counter += 1
if guess == target:
print('Got "{}" after {} tests'.format(guess, str(counter)))
return True, counter
return False, counter
else:
for c in ALPHABET:
target_found, counter = brute_force_guesser(target, guess + c, counter)
if target_found:
return True, counter
return False, counter
brute_force_guesser('dog') # => Got "dog" after 2399 tests
brute_force_guesser('doggy') # => Got "doggy" after 1621229 tests
Here is my full answer, sorry if it's not neat, I'm still new to coding in general. The credit goes to #JohnColeman for the great idea of using bases.
import math
global guess
pasw = str(input('Input password: '))
chars = 'abcdefghijklmnopqrstuvwxyz' #only limeted myself to lowercase for simplllicity.
base = len(chars)+1
def cracker(pasw):
guess = ''
tests = 1
c = 0
m = 0
while True:
y = tests
while True:
c = y % base
m = math.floor((y - c) / base)
y = m
guess = chars[(c - 1)] + guess
print(guess)
if m == 0:
break
if guess == pasw:
print('Got "{}" after {} tests'.format(guess, str(tests)))
break
else:
tests += 1
guess = ''
cracker(pasw)
input()
import itertools
import string
def guess_password(real):
chars = string.ascii_lowercase + string.digits
attempts = 0
for password_length in range(1, 20):
for guess in itertools.product(chars, repeat=password_length):
attempts += 1
guess = ''.join(guess)
if guess == real:
return 'the password is {}, found in {} guesses.'.format(guess, attempts)
print(guess, attempts)
print(guess_password('abc'))

Python Recursion Double letters

So what the task is, is that your supposed to write a recursion function that counts the amount of "double" letters in a string, So for example the string "hmmm" would return 1 and the string "hmmmm" would return 2 and that a string "abb" would return 1. My code is here:
def num_double_letters(astr):
if astr == "" or len(astr) == 1:
return 0
elif len(astr) == 2:
if astr[0] == astr[1]:
return 1 + num_double_letters(astr[1:])
else:
return 0 + num_double_letters(astr[1:])
elif astr[0] != astr[1]:
return 0 + num_double_letters(astr[1:])
elif astr[0] == astr[1] != astr[2]:
return 1 + num_double_letters(astr[1:])
elif astr[0] == astr[1] == astr[2]:
return 0 + num_double_letters(astr[1:])
My problem is that a string with 4 same letters = 1 when its supposed to = 2. And also is there a cleaner way to do this?
I think you've made it a bit complicated for yourself... there's no need to go deeper into the recursion once the length of your string is 2, and you want to advance by 2, not 1 when you find a double to count the way I think you do. Try this:
def num_double_letters(astr):
if astr == "" or len(astr) == 1:
return 0
elif len(astr) == 2:
if astr[0] == astr[1]:
return 1
else:
return 0
elif astr[0] != astr[1]:
return 0 + num_double_letters(astr[1:])
elif astr[0] == astr[1]:
return 1 + num_double_letters(astr[2:])
print(num_double_letters('hmm'))
print(num_double_letters('hmmm'))
print(num_double_letters('hmmmm'))
Output:
1
1
2
You might consider the following more Pythonic and concise:
def num_double_letters(astr):
if len(astr) < 2:
return 0
if astr[0] == astr[1]:
return 1 + num_double_letters(astr[2:])
return num_double_letters(astr[1:])

Categories

Resources