Skip to content

Commit cb088ef

Browse files
Added session 4 answers
Signed-off-by: Chandra Prakash S <[email protected]>
1 parent 9317e75 commit cb088ef

9 files changed

+411
-1
lines changed

Session-4/arifplaysvolleyball.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Problem Description:
2+
Arif likes to play volley ball. He found some statistics of matches which described who won the points in order.
3+
A game shall be won by the player first scoring 11 points except in the case when both players have 10 points each, then the game shall be won by the first player subsequently gaining a lead of 2 points.
4+
Could you please help the Arif find out who the winner was from the given statistics? (It is guaranteed that statistics represent always a valid, finished match.)
5+
Constraints:
6+
1 ≤ T ≤ 1000
7+
1 ≤ length(matchscenario) ≤ 100
8+
Input Format:
9+
The first line of the input contains an integer 'T', denoting the number of test cases. The description of 'T' test cases follows.
10+
Each test case consist a binary string 'S', which describes a match.
11+
'0' means Arif lose a point, whereas '1' means he won the point.
12+
Output Format:
13+
Print the output on a separate line a string describing who won the match.
14+
If Arif won then print "WIN" (without quotes), otherwise print "LOSS" (without quotes).
15+
16+
Logical Test Cases
17+
Test Case 1
18+
INPUT (STDIN)
19+
4
20+
0101111111111
21+
10110101101010
22+
11100101101101
23+
EXPECTED OUTPUT
24+
WIN
25+
LOSS
26+
Test Case 2
27+
INPUT (STDIN)
28+
6
29+
0001111111111
30+
11100000000000
31+
1001111111111
32+
1000110100101
33+
1111001110011
34+
1000100111011
35+
EXPECTED OUTPUT
36+
LOSS
37+
WIN
38+
LOSS"""
39+
40+
# function to determine the winner of a single match
41+
def determine_winner(match):
42+
arif_score = 0
43+
opponent_score = 0
44+
score_set = set() # use a set to keep track of the scores
45+
for i in range(len(match)):
46+
if match[i] == '1':
47+
arif_score += 1
48+
else:
49+
opponent_score += 1
50+
if (arif_score >= 11 or opponent_score >= 11) and abs(arif_score - opponent_score) >= 2:
51+
if arif_score > opponent_score:
52+
return "WIN"
53+
else:
54+
return "LOSS"
55+
score_set.add((arif_score, opponent_score)) # add the current score to the set
56+
# check if the match ended with a score less than 11-10
57+
last_score = list(score_set)[-1]
58+
if last_score[0] >= 11 or last_score[1] >= 11:
59+
if last_score[0] > last_score[1]:
60+
return "WIN"
61+
else:
62+
return "LOSS"
63+
else:
64+
return "LOSS"
65+
# main function
66+
if __name__ == "__main__":
67+
t=int(input()) # number of test cases
68+
for i in range(t):
69+
matchscenario=str(input()) # input string describing match
70+
result = determine_winner(matchscenario)
71+
print(result) # print the winner of the match

Session-4/aronhasnum.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Problem Description:
2+
Aaron has a number D containing only digits 0's and 1's. He wants to make the number to have all the digits same. For that, he will change exactly one digit, i.e. from 0 to 1 or from 1 to 0.
3+
If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then output "Yes", else print "No" (quotes for clarity)
4+
Constraints:
5+
1 ≤ T ≤ 10
6+
1 ≤ Length of the number D ≤ 10
7+
Input Format:
8+
The first line will contain an integer 'T' representing the number of test cases.
9+
Each test case contain a number made of only digits 1's and 0's on newline
10+
Output Format:
11+
Print T lines with a "Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.
12+
13+
Logical Test Cases
14+
Test Case 1
15+
INPUT (STDIN)
16+
3
17+
1010
18+
101
19+
100
20+
EXPECTED OUTPUT
21+
NO
22+
YES
23+
Test Case 2
24+
INPUT (STDIN)
25+
6
26+
010
27+
1101
28+
0011
29+
101110
30+
0001
31+
0010
32+
EXPECTED OUTPUT
33+
YES
34+
NO
35+
NO
36+
YES"""
37+
38+
def check_all_same(s):
39+
# Count number of 0s and 1s
40+
count_0 = s.count('0')
41+
count_1 = s.count('1')
42+
43+
# If all digits are already same, return Yes
44+
if count_0 == len(s) or count_1 == len(s):
45+
return "YES"
46+
# If there is only one 0 or one 1, return Yes
47+
if count_0 == 1 or count_1 == 1:
48+
return "YES"
49+
# If there is more than one 0 and more than one 1, return No
50+
if count_0 > 1 and count_1 > 1:
51+
return "NO"
52+
# If there is exactly one 0 and more than one 1, or exactly one 1 and more than one 0, return Yes
53+
else:
54+
return "YES"
55+
if __name__ == "__main__":
56+
# Get number of test cases
57+
T = int(input())
58+
# Loop over all test cases
59+
for i in range(T):
60+
# Get input string
61+
s=str(input())
62+
# Call function to check if all digits can be made same by flipping one digit
63+
result = check_all_same(s)
64+
# Print the result
65+
print(result)

Session-4/geppeto.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Question description
2+
A long time ago, there resided a person called Geppetto. He was old and lived by himself. He had no family of his own. Geppetto was a carpenter. He loved to create stuffs from wood. One day, he thought to make a puppet out of wood. He said, “I will make a little boy and will call him ‘Pinocchio.’ First, Geppetto made some wooden legs and arms. After that, he made the body, and he included hands and feet. Finally, he made a little boy’s head. Geppetto made Pinocchio’s eyes, mouth, and nose. After that, he made his ears. Geppetto worked through out day and night on his wooden puppet. He said to himself, “I wish Pinocchio were a real boy.” A fairy heard Geppetto’s wish. She decided to grant his wish and make the wooden puppet come to life. She said to Pinocchio, “You must promise to be a good and honest boy.” The next day, Geppetto was very happy to hear Pinocchio talk. He loved his wooden son very much. Geppetto smiled. “Now you can go to school with all the other little boys!”. At school, English teacher gave the task to Pinocchio that find the number of vowels in the given set of strings. Can you help Pinocchio to find the number of vowels in the given set of strings.
3+
Constraints
4+
1≤t≤5
5+
Input Format:
6+
Refer the Test cases
7+
Output Format:
8+
Refer the Test cases
9+
10+
Logical Test Cases
11+
Test Case 1
12+
INPUT (STDIN)
13+
3
14+
invertible
15+
inverse
16+
reverse
17+
EXPECTED OUTPUT
18+
4
19+
3
20+
Test Case 2
21+
INPUT (STDIN)
22+
4
23+
Geppetto
24+
Fairy
25+
Pinocchio
26+
Teacher
27+
EXPECTED OUTPUT
28+
3
29+
2
30+
4
31+
3"""
32+
33+
# Reading the number of test cases
34+
t = int(input())
35+
# Looping through each test case
36+
for i in range(t):
37+
# Reading the input string
38+
str1=str(input())
39+
# Initializing the vowel count
40+
count = 0
41+
# Looping through each character in the string
42+
for j in range(len(str1)):
43+
# Checking if the character is a vowel
44+
if str1[j] in "aeiouAEIOU":
45+
count += 1
46+
# Printing the vowel count for the current string
47+
print(count)

Session-4/janakireverse.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Question description
2+
Janaki had the extra ability that, she will spell the given set of strings in reverse order. Can you verify the strings?
3+
Constraints
4+
1≤t≤5
5+
Input Format
6+
Refer the test cases
7+
Output Format
8+
Refer the test cases
9+
10+
Logical Test Cases
11+
Test Case 1
12+
INPUT (STDIN)
13+
2
14+
Raja
15+
Radha
16+
EXPECTED OUTPUT
17+
ajaR
18+
ahdaR
19+
Test Case 2
20+
INPUT (STDIN)
21+
3
22+
President
23+
Prime Minister
24+
Governor
25+
EXPECTED OUTPUT
26+
tnediserP
27+
retsiniM emirP
28+
ronrevoG"""
29+
30+
# Prompt inputs
31+
t=int(input())
32+
# Loop through each test case
33+
for i in range(t):
34+
# Get the string
35+
test_string=str(input())
36+
37+
# Reverse the string
38+
string_reversed = test_string[::-1]
39+
40+
# Print the reversed string
41+
print(string_reversed)

Session-4/janakisetpassword.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Question description
2+
Janaki set the password of her locker that the possible set of substrings from the string. Can you help her to set the password?
3+
Constraints
4+
1<len(string)≤10
5+
Input Format:
6+
Refer the test cases
7+
Output Format:
8+
Refer the test cases
9+
10+
Logical Test Cases
11+
Test Case 1
12+
INPUT (STDIN)
13+
abc
14+
EXPECTED OUTPUT
15+
a
16+
c
17+
Test Case 2
18+
INPUT (STDIN)
19+
abcd
20+
EXPECTED OUTPUT
21+
a
22+
ab
23+
abc
24+
abcd
25+
b
26+
bc
27+
bcd
28+
c
29+
cd
30+
d"""
31+
32+
s=str(input())
33+
# Printing all substrings of the string s
34+
for i in range(len(s)):
35+
for j in range(i+1, len(s)+1):
36+
print(s[i:j])

Session-4/lokesh.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""Problem Description:
2+
Lokesh usually likes to play cricket, but now, he is bored of playing it too much, so he is trying new games with strings.
3+
Lokesh's friend Tina gave him binary strings S and R, each with length N, and told him to make them identical.
4+
However, unlike Tina, Lokesh does not have any superpower and Tina lets Lokesh perform only operations of one type: choose any pair of integers (i,j) such that 1≤i,j≤N and swap the i-th and j-th character of S.
5+
He may perform any number of operations (including zero).
6+
For Lokesh, this is much harder than cricket and he is asking for your help.
7+
Tell him whether it is possible to change the string S to the target string R only using operations of the given type.
8+
Constraints:
9+
1≤T≤400
10+
1≤N≤100
11+
|S|=|R|=N
12+
S and R will consist of only '1' and '0'
13+
Input Format:
14+
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
15+
The first line of each test case contains a single integer N.
16+
The second line contains a binary string S.
17+
The third line contains a binary string R.
18+
Output Format:
19+
For each test case, print a single line containing the string "YES" if it is possible to change S to R or "NO" if it is impossible (without quotes).
20+
21+
Logical Test Cases
22+
Test Case 1
23+
INPUT (STDIN)
24+
3
25+
1000101
26+
0110100
27+
1101
28+
0010
29+
100111
30+
010011
31+
EXPECTED OUTPUT
32+
YES
33+
NO
34+
Test Case 2
35+
INPUT (STDIN)
36+
4
37+
6
38+
100110
39+
111010
40+
9
41+
001000101
42+
111010010
43+
7
44+
0100010
45+
1101011
46+
5
47+
10110
48+
11010
49+
EXPECTED OUTPUT
50+
NO
51+
NO
52+
NO
53+
YES"""
54+
def can_be_identical(n, s1, s2):
55+
s1_count = s1.count('1')
56+
s2_count = s2.count('1')
57+
if s1_count != s2_count:
58+
return "NO"
59+
else:
60+
return "YES"
61+
62+
t = int(input())
63+
for i in range(t):
64+
n=int(input())
65+
s1=str(input())
66+
s2=str(input())
67+
print(can_be_identical(n, s1, s2))

Session-4/loremipsum.py

-1
This file was deleted.

Session-4/sudan&siva.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Question description
2+
Sudan and Siva are school mates. They challenge each other to play a game that to arrange string characters such that lowercase letters should come first
3+
Constraints
4+
1<t<5
5+
Input Format:
6+
Refer the test cases
7+
Output Format:
8+
Refer the test cases
9+
10+
Logical Test Cases
11+
Test Case 1
12+
INPUT (STDIN)
13+
2
14+
Radhakrishnan
15+
Radhai
16+
EXPECTED OUTPUT
17+
adhakrishnanR
18+
adhaiR
19+
Test Case 2
20+
INPUT (STDIN)
21+
4
22+
Saravanan
23+
Manikandan
24+
Vignesh
25+
Subash
26+
EXPECTED OUTPUT
27+
aravananS
28+
anikandanM
29+
igneshV
30+
ubashS"""
31+
32+
# Taking input for the number of test cases
33+
t = int(input())
34+
# Looping through each test case
35+
for i in range(t):
36+
# Taking input for the string
37+
str1=str(input())
38+
39+
# Sorting the characters in the string using the sorted() function and the key parameter
40+
sorted_string = sorted(str1, key=lambda x: x.isupper())
41+
42+
# Joining the sorted characters back to form the final string
43+
final_string = ''.join(sorted_string)
44+
45+
# Printing the final string
46+
print(final_string)

0 commit comments

Comments
 (0)