alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
Copy the code above into your program but write all the other code for this assignment yourself.
Write a function called has_duplicates that takes a string parameter and returns True if the string has any repeated characters. Otherwise, it should return False.
QUESTIONS NOT COMPLETE BUT IT WILL BE CONTINUE.....
Write a program to input a number or word from the user and find its reverse. If the reverse is the same as the input then it is a palindrome. Check if the text is a palindrome or not.
m, n = input('Enter the values of M and N separated by a space: ').split()
m = int(m)
n = int(n)
matrix_str = input('Enter all matrix values separated by a space: ').split()
matrix = [matrix_str[n*i:n*(i+1)] for i in range(m)]
for i in range(n):
for j in range(i+1):
try:
print(matrix[j][i-j],end=' ')
except:
break
print('')
for i in range(1,m):
for j in range(i+1):
try:
print(matrix[i+j][-1-j],end=' ')
except:
break
print('') In this answer why did we use for i in range(m) while getting matrix
can you please explain to me why we used it like that and can we use it like that
m, n = input('Enter the values of M and N separated by a space: ').split()
m = int(m)
n = int(n)
matrix_str = input('Enter all matrix values separated by a space: ').split()
matrix = [matrix_str[n*i:n*(i+1)] for i in range(m)]
for i in range(n):
for j in range(i+1):
try:
print(matrix[j][i-j],end=' ')
except:
break
print('')
for i in range(1,m):
for j in range(i+1):
try:
print(matrix[i+j][-1-j],end=' ')
except:
break
print('')
In this answer why did we use for i in range(m) while getting the matrix
1. Write a program to input co-efficients of a, b and c of a quadratic equation ax2 + bx + c = 0, where a = 0. Discriminant, D = b2- 4ac.If discriminant is 0 then print there is exactly one real root, if discriminant is positive then print there are two distinct roots, if discriminant is negative then print then there are no real roots.
1. Write a program to input a year in 4-digit form and check whether it is a leap year or not and display proper message.
Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it.
def invert_dict(d):
inverse = dict()
for key in d:
val = d[key]
if val not in inverse:
inverse[val] = [key]
else:
inverse[val].append(key)
return inverse
Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.
Run your modified invert_dict function on your dictionary. Print the original dictionary and the inverted one.
Include your output
Describe what is useful about your dictionary. Then describe whether the inverted dictionary is useful or meaningful, and why.
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source.
Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
help please check! i can run the code, but i can't input and output words and numbers.
dict1 {'a':2,'b':2,'c':2,'d':3,'e':3,'f':3,'g':4,'h':4,'i':4,'j':5,'k':5,'l':5,'m':6,'n':6,'o':6,'p':7,'q':7,'r':7,'s':7,'t':8,'
u': 8,'v':8,'w':9,'x':9,'y':9,'z':9}
def convert(word):
x = 0 for i in range(0,len(word)):
ch = word[i] x = x * 10 + dict1[ch]
return x n = int(input())
words =[] T9=[]
for i in range(0,n): words.append(input())
T9.append(convert(words[i])) case1=[]
t = int(input()) for i in range(0,t):
cases1.append(input()) for i in range(0,t): x = cases1[i].split(' ')
y = 1 print("Message #",(i + 1), ",:", end = " ")
for j in x: y = y * T9.count(int(j))
if y == 0: print("Not valid. Please enter a valid text")
elif y!=1: print("There are ",y," possible messages")
else: for j in x:
print(words[T9.index(int(j))],end = " ")
Reverse Alternate Words in Sentence
Given a sentence, write a program to reverse the alternate words in the sentence and print it.
Reverse the first word and respective alternative words.
The input will be a single line containing a sentence.
The output should be a single line containing the sentence with the alternate words reversed.
For example, if the given sentence is
Sun rises in the east and sets in the west The alternate words which need to be reversed are (Sun, in, east, sets, the), by reversing these words the output should be nuS rises ni the tsae and stes in eht west
Sample Input 1
Sun rises in the east and sets in the west
Sample Output 1
nuS rises ni the tsae and stes in eht west
Sample Input 2
The car turned the corner
Sample Output 2
ehT car denrut the renroc