Hi its my first week learning python and I am making a hangman game
I opened a list of words from a .txt file and then going through the words to remove any words that are shorter than 4 or longer than 7 to make an easy list or words.
When i run the code it removes some of the words but not all of them. I don't understand why and can't seem to figure it out.
Here is the code:
easy_hangman_words = hangman_words("easyhangman.txt")
for x in easy_hangman_words:
print(" {} length {} ".format(x, len(x)))
for word in easy_hangman_words:
if len(word) < 4 or len(word) > 7:
print(word)
easy_hangman_words.remove(word)
I checked the length of each word before if statement and here is a sample:
represent length 9,
Republican length 10,
require length 7 ,
research length 8 ,
resource length 8 ,
respond length 7 ,
response length 8 ,
responsibility length 14 ,
rest length 4 ,
result length 6 ,
return length 6 ,
reveal length 6 ,
rich length 4 ,
right length 5 ,
rise length 4 ,
risk length 4 ,
road length 4 ,
rock length 4 ,
role length 4 ,
room length 4 ,
rule length 4 ,
run length 3 ,
safe length 4 ,
same length 4 ,
save length 4 ,
say length 3 ,
then printed the words in the if loop and here is what i got
represent
research
response
run
say
it seems to have missed Republican, responsibility, and resource.
Any ideas?
You're both reading (in the for loop) and modifying (the remove) the list! This can cause python's for loops to get pretty wonky. There are more pythonic ways to do this, but this should fix the traversal problem.
You should try something like this:
remove_words = []
for word in easy_hangman_words:
if len(word) < 4 or len(word) > 7:
print(word)
remove_words.append(word)
for remove in remove_words:
easy_hangman_words.remove(word)
Related
Example 1:
Input:
n = 1
Output: 1
Explanation: Digital root of 1 is 1
Example 2:
Input:
n = 99999
Output: 9
Explanation: Sum of digits of 99999 is 45
which is not a single digit number, hence
sum of digit of 45 is 9 which is a single
digit number.
Could someone help what is the time complexity of my code? I think its O(loglog(N)) but not sure.
def sumOfDigits(n):
if n==0:
return 0
else:
return int(n%10) + sumOfDigits(n//10)
def digitalRoot(n):
ans = n
if n<=9:
return n
else:
while ans>9:
ans = sumOfDigits(ans)
return ans
Let's calculate the complexity for the first step
If an algorithm depends on the number of digits it contains the time complexity of such algorithm is:
O(log10(n))
This is because we represent numbers in base 10 notation.
For example, this would make the relation crystal clear:
O(log10(100)) = 2
O(log10(1000)) = 3
O(log10(10000)) = 4
Now this answers the question to some extent, if we were only adding all digits once, we'd stop here.
But since we're not, let's move forward. Now if all the digits are added once, we again add the digits of that resultant number. Making it a convergent series.
Thus the answer could be:
O(log10(n)) + O(log10(log10(n))) + O(log10(log10(log10(n)))) + ...
This is my best estimation for the upper bound of complexity.
here is my code... It's considering only 8 and 88 among 1 to 100 which aren't karpekar numbers...failing at if condition(s==n)
def kaprekarNumbers(p, q):
for i in range(p,q+1):
n=i
m=str(i*i);
sl1=m[:int(len(m)/2)]
sl2=m[int(len(m)/2):]
if(sl2==""):
sl2=0
s=int(sl2)+int(sl2)
print(s==n)
if s==n:
print(i)
Using strings to process numbers is not usually a good idea.
You can get the number of digits of a number n with
math.ceil(math.log10(n))
You can get the last a digits of a number n with
n % a
(See: How does % work in Python?)
You can get the first a digits of a number n with
p // (10 ** a)
Those would be useful for base-10 Kaprekar numbers.
[Please note, I do not have a copy of Python to hand to check those.]
my code is:
n=int(input())
list_1 = []
for i in range(n):
list_1.append(input())
list_2=[]
#print(list_1)
while list_1:
minimum = list_1[0]
for x in list_1:
if x < minimum:
minimum = x
list_2.append(minimum)
list_1.remove(minimum)
print (' '.join(map(str, list_2)))
all output come correct but incorrect come in some input like
4
10
3
7
6
please help
Your list 'list_1' is a list of strings, and for strings minimums work in a different manner. For example, '10' < '3' is True.
Change the line:
list_1.append(input())
To:
list_1.append(int(input()))
The first thing you should do when posting questions here is properly explain your problem, and what the code does.
Now for your question, Mono found the issue in your code, but you should know that you do not need all this to sort a list of numbers. It already exists in the language. Use the sort() function on the list, like this:
print("This script will ask you for numbers and print them back to you in order.")
print("Enter how many numbers you will input: ", end="")
n=int(input())
list_1 = []
print("Please type each number.")
for i in range(n):
print(" Number", i, ": ", end='')
list_1.append(int(input()))
list_1.sort()
print("These are your numbers, in order:")
print (' '.join(map(str, list_1)))
The output is:
This script will ask you for numbers and print them back to you in order.
Enter how many numbers you will input: 4
Please type each number.
Number 0 : 10
Number 1 : 2
Number 2 : 8
Number 3 : 3
These are your numbers, in order:
2 3 8 10
I have 2 question regarding searching for strings in MATLAB
If I have to find a string in a cell array of strings I can do the following to get the location of 'PO' in the cell array
find(strcmpi({'PO','FOO','PO1','FOO1','PO1','PO'},'PO'))
% 1 6
But, I really want to search for multiple strings ({'PO1', 'PO'}) at the same time (not using a for loop). What is the best way to do this?
Is there any function like histc() which can tell me how many times the string has occurred. Again for one string, I could do:
length(strfind({'PO','FOO','PO1','FOO1','PO1','PO'},'PO'))
But this obviously doesn't work for multiple strings at a time.
If you want to find multiple strings, then just use the second output of ismember instead to tell you which string it is. If you really need case-insensitive matching, I've added the upper call to force all inputs to be upper-case. You can omit this if you think it's already uppercase.
data = {'PO','FOO','PO1','FOO1','PO1','PO', 'PO'};
[tf, inds] = ismember(upper(data), {'PO1', 'PO'});
% 2 0 1 0 1 2 2
You can then use the second output to determine which string was found where:
% PO1 Occurrences
find(inds == 1)
% 3 5
% PO Occurrences
find(inds == 2)
% 1 6 7
If you want the equivalent of histc, you can use accumarray to do that. We can pass it all of the values of inds that are non-zero (i.e. the ones that you were actually searching for).
accumarray(inds(tf).', ones(sum(tf), 1))
% 2 3
If instead you want to get the histogram of all strings (not just the ones you're searching for) you could do the following:
[strings, ~, inds] = unique(data, 'stable');
occurrences = accumarray(inds, ones(size(inds)));
% 'PO' [3]
% 'FOO' [1]
% 'PO1' [2]
% 'FOO1' [1]
L_counts will keep the count for 'a' at position 0, the count for 'b' at position 1, and so on. i must have a way to know what English letter corresponds to each position in L_counts. im not quite understanding the instruction so if i create a empty list or in the list put 0 - 2.
L_count = []
L_counts = [0,1,2,3.. so on]
Any problem with the following?
L_Count = [0]*26