Word Rearrange: In word Rearrange players try to score points by forming words using the letters from a 6-letters scrambled word.Given some guessed words,find the total No.of points the players scored in a particular round using the following rubric. 3-letter words are 1 pt, 4-letter words are 2 pts, 5-letter words are 3 pts, 6-letter words are 4 pts+50 pt bonus. A guessed word is invalid if the characters are not present in the scrambled word.Notes-that invalid words count as 0. your task is to find out the final score.
INPUT: The first line contains a string representing a scrambled word. The Second Line contains some spaces-separated strings representing words guessed by the player.
INPUT:
rceast
cat create sat
OUTPUT:
2
INPUT:
tacren
trance recant
OUTPUT:
108
while True:
scrambled_w = input('Please, input word => ').lower()
if scrambled_w.isdigit() == False and len(scrambled_w) == 6:
break
else:
print('Please, input word which lenght = 6')
guess_w = input('Please, input words whose characters can be in the scrambled word\n').split()
dct_scr = {}
points = {
3: 1,
4: 2,
5: 3,
6: 4
}
total = 0
flag = True
for el in scrambled_w:
dct_scr[el] = dct_scr.get(el, 0) + 1
for el in guess_w:
if el.isdigit() == False and 3 <= len(el) <= 6:
dct_guess = {}
for c in el:
dct_guess[c] = dct_guess.get(c, 0) + 1
for key, value in dct_guess.items():
if key in dct_scr:
if value != dct_scr[key]:
flag = False
break
else:
flag = True
else:
break
if flag:
total += points[len(el)]
if len(el) == 6:
total += 50
else:
continue
print('Your points =', total)
Rearrange Numbers in String
Given a string, write a program to re-arrange all the numbers appearing in the string in decreasing order. Note: There will not be any negative numbers or numbers with decimal part.
The input will be a single line containing a string.
The output should be a single line containing the modified string with all the numbers in string re-ordered in decreasing order.
For example, if the given string is "I am 5 years and 11 months old", the numbers are 5, 11. Your code should print the sentence after re-ordering the numbers as "I am 11 years and 5 months old".
Sample Input
I am 5 years and 11 months old
Sample Output
I am 11 years and 5 months old
Sample Input
python4 anjali25
Sample Output
python25 anjali4
Sample Input
1python254
Sample Output
254python1
Sample Input
-1pyth-4on 5lear-3ning-2
Sample Output
5pyth4on 3lear2ning1
Word Rearrange: In word Rearrange players try to score points by forming words using the letters from a 6-letters scrambled word.Given some guessed words,find the total No.of points the players scored in a particular round using the following rubric. 3-letter words are 1 pt, 4-letter words are 2 pts, 5-letter words are 3 pts, 6-letter words are 4 pts+50 pt bonus. A guessed word is invalid if the characters are not present in the scrambled word.Notes-that invalid words count as 0. your task is to find out the final score.
INPUT: The first line contains a string representing a scrambled word. The Second Line contains some spaces-separated strings representing words guessed by the player.
INPUT:
rceast
cat create sat
OUTPUT:
2
INPUT:
tacren
trance recant
OUTPUT:
108
while True:
scrambled_w = input('Please, input word => ').lower()
if scrambled_w.isdigit() == False and len(scrambled_w) == 6:
break
else:
print('Please, input word which lenght = 6')
guess_w = input('Please, input words whose characters can be in the scrambled word\n').split()
dct_scr = {}
points = {
3: 1,
4: 2,
5: 3,
6: 4
}
total = 0
flag = True
for el in scrambled_w:
dct_scr[el] = dct_scr.get(el, 0) + 1
for el in guess_w:
if el.isdigit() == False and 3 <= len(el) <= 6:
dct_guess = {}
for c in el:
dct_guess[c] = dct_guess.get(c, 0) + 1
for key, value in dct_guess.items():
if key in dct_scr:
if value != dct_scr[key]:
flag = False
break
else:
flag = True
else:
break
if flag:
total += points[len(el)]
if len(el) == 6:
total += 50
else:
continue
print('Your points =', total)
Rearrange Numbers in String
Given a string, write a program to re-arrange all the numbers appearing in the string in decreasing order. Note: There will not be any negative numbers or numbers with decimal part.
The input will be a single line containing a string.
The output should be a single line containing the modified string with all the numbers in string re-ordered in decreasing order.
For example, if the given string is "I am 5 years and 11 months old", the numbers are 5, 11. Your code should print the sentence after re-ordering the numbers as "I am 11 years and 5 months old".
Sample Input
I am 5 years and 11 months old
Sample Output
I am 11 years and 5 months old
Sample Input
python4 anjali25
Sample Output
python25 anjali4
Sample Input
1python254
Sample Output
254python1
Sample Input
-1pyth-4on 5lear-3ning-2
Sample Output
5pyth4on 3lear2ning1
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a String of words, assign an index to each word.
Input : test_str = ‘geekforgeeks is best’
Output : {0: ‘geekforgeeks’, 1: ‘is’, 2: ‘best’}
Explanation : Index assigned to each word.
Input : test_str = ‘geekforgeeks best’
Output : {0: ‘geekforgeeks’, 1: ‘best’}
Explanation : Index assigned to each word.
Method #1 : Using enumerate() + dict() + split()
In this, we first perform task of split() and then add index component to map each word with index using enumerate().
Python3
test_str = 'geekforgeeks is best for geeks'
print("The original string is : " + str(test_str))
res = dict(enumerate(test_str.split()))
print("The Assigned Sequence : " + str(res))
Output
The original string is : geekforgeeks is best for geeks The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}Method #2 : Using zip() + count() + dict()
In this, the count() component renders the index logic and pairing of each word to index is done using zip().
Python3
from itertools import count
test_str = 'geekforgeeks is best for geeks'
print("The original string is : " + str(test_str))
res = dict(zip(count(), test_str.split()))
print("The Assigned Sequence : " + str(res))
Output
The original string is : geekforgeeks is best for geeks The Assigned Sequence : {0: 'geekforgeeks', 1: 'is', 2: 'best', 3: 'for', 4: 'geeks'}The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)