Skip to content

Commit 32ace90

Browse files
23 sept....
1 parent 6965fe9 commit 32ace90

25 files changed

+1944
-0
lines changed

10X/Python/Sorting/Add String.py

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
'''
2+
Add Strings
3+
Description
4+
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
5+
6+
Note:
7+
8+
The length of both num1 and num2 is < 5100.
9+
10+
Both num1 and num2 contains only digits 0-9.
11+
12+
Both num1 and num2 does not contain any leading zero.
13+
14+
You MUST NOT convert the inputs to integer directly.
15+
16+
Complete the addStrings function. It takes the input as two strings & you need to return the string (which will same as the addition of two number as integers).
17+
18+
'''
19+
########################################################################
20+
# Complete this addStrings() function
21+
# converting string into list and then adiition as
22+
"""
23+
def digit(d):
24+
if d=="0":
25+
return 0
26+
elif d=="1":
27+
return 1
28+
elif d=="2":
29+
return 2
30+
elif d=="3":
31+
return 3
32+
elif d=="4":
33+
return 4
34+
elif d=="5":
35+
return 5
36+
elif d=="6":
37+
return 6
38+
elif d=="7":
39+
return 7
40+
elif d=="8":
41+
return 8
42+
elif d=="9":
43+
return 9
44+
def addStrings(num1, num2):
45+
### Here num1 & num2 are strings [Return the addition of these two strings as string]
46+
a1=[]
47+
a2=["0"]
48+
a=""
49+
no=0
50+
if len(num1)>=len(num2):
51+
a1=["0"]*len(num1)
52+
a2=["0"]*len(num1)
53+
elif len(num2)>len(num1):
54+
a1=["0"]*len(num2)
55+
a2=["0"]*len(num2)
56+
for i in range(len(num1)):
57+
a1[len(a1)-1-i]=num1[len(num1)-1-i]
58+
for i in range(len(num2)):
59+
a2[len(a2)-1-i]=num2[len(num2)-1-i]
60+
61+
c=0#carry
62+
63+
for i in range(len(a1)):
64+
n=digit(a1[len(a1)-1-i])+digit(a2[len(a2)-1-i])+c
65+
if n==10:
66+
c=1
67+
n-=10
68+
elif n>10:
69+
c=1
70+
n-=10
71+
else:
72+
c=0
73+
a+=str(n)
74+
no+=n*(10**i)
75+
if c==1:
76+
a+=str(c)
77+
no+=c*(10**len(a1))
78+
b=a[::-1]#String Formate
79+
print(b)
80+
return no
81+
## Do not change anything below this line:
82+
83+
for _ in range(int(input())):
84+
n1, n2 = input().split()
85+
print(addStrings(n1,n2))
86+
87+
"""
88+
89+
def digit(d):
90+
if d=="0":
91+
return 0
92+
elif d=="1":
93+
return 1
94+
elif d=="2":
95+
return 2
96+
elif d=="3":
97+
return 3
98+
elif d=="4":
99+
return 4
100+
elif d=="5":
101+
return 5
102+
elif d=="6":
103+
return 6
104+
elif d=="7":
105+
return 7
106+
elif d=="8":
107+
return 8
108+
elif d=="9":
109+
return 9
110+
def addStrings(num1, num2):
111+
### Here num1 & num2 are strings [Return the addition of these two strings as string]
112+
a1=[]
113+
a2=[]
114+
no=0
115+
if len(num1)>=len(num2):
116+
a1=["0"]*len(num1)
117+
a2=["0"]*len(num1)
118+
elif len(num2)>len(num1):
119+
a1=["0"]*len(num2)
120+
a2=["0"]*len(num2)
121+
for i in range(len(num1)):
122+
a1[len(a1)-1-i]=num1[len(num1)-1-i]
123+
for i in range(len(num2)):
124+
a2[len(a2)-1-i]=num2[len(num2)-1-i]
125+
126+
c=0#carry
127+
128+
for i in range(len(a1)):
129+
n=digit(a1[len(a1)-1-i])+digit(a2[len(a2)-1-i])+c
130+
if n==10:
131+
c=1
132+
n-=10
133+
elif n>10:
134+
c=1
135+
n-=10
136+
else:
137+
c=0
138+
no+=n*(10**i)
139+
if c==1:
140+
no+=c*(10**len(a1))
141+
return no
142+
## Do not change anything below this line:
143+
for _ in range(int(input())):
144+
n1, n2 = input().split()
145+
print(addStrings(n1,n2))
146+
147+
#### 83% ####
148+

10X/Python/Sorting/Code words.py

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""
2+
Code Words
3+
Description
4+
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
5+
6+
For convenience, the full table for the 26 letters of the English alphabet is given below:
7+
8+
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
9+
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-..--...", (which is the concatenation "-.-." + ".-" + "-..."). We'll call such a concatenation, the transformation of a word.
10+
11+
Return the number of different transformations among all words we have.
12+
13+
Input format
14+
First line contains a positive integer n, denoting the number of test cases. It is followed by n lines. Each of the n lines contains space separated words.
15+
16+
Output format
17+
n lines containing the number of different transformations among all words we have.
18+
19+
Sample input
20+
1
21+
gin zen gig msg
22+
Sample output
23+
2
24+
Explanation
25+
The transformation of each word is:
26+
27+
"gin" -> "--...-."
28+
29+
"zen" -> "--...-."
30+
31+
"gig" -> "--...--."
32+
33+
"msg" -> "--...--."
34+
35+
There are 2 different transformations, "--...-." and "--...--.".
36+
37+
Note
38+
The length of words will be at most 100000
39+
40+
Each words[i] will have length in range [1, 12].
41+
42+
words[i] will only consist of lowercase letters.
43+
44+
"""
45+
#a=97
46+
#z=97+25=122 ord("z")
47+
########################### BY USING DICT ######################
48+
49+
'''
50+
# your code goes here
51+
def morsh(ss):
52+
dic={"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."}
53+
s=""
54+
for i in range(len(ss)):
55+
if ss[i] in dic:
56+
s+=dic[ss[i]]
57+
return s
58+
59+
def check(ar):
60+
dic1={}
61+
for i in range(len(ar)):
62+
temp=morsh(ar[i])
63+
if temp in dic1:
64+
dic1[temp]+=1
65+
else:
66+
dic1[temp]=1
67+
return len(dic1)
68+
69+
for _ in range(int(input())):
70+
ar=input().split()
71+
print(check(ar))
72+
73+
'''
74+
################### FOR ONLY small(a-z) #########################
75+
'''
76+
def morshword(ss):
77+
dic={"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--","n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--..","A":".-","B":"-...","C":"-.-.","D":"-..","E":".","F":"..-.","G":"--.","H":"....","I":"..","J":".---","K":"-.-","L":".-..","M":"--","N":"-.","O":"---","P":".--.","Q":"--.-","R":".-.","S":"...","T":"-","U":"..-","V":"...-","W":".--","X":"-..-","Y":"-.--","Z":"--.."}
78+
s=""
79+
for i in range(len(ss)):
80+
if ss[i] in dic:
81+
s+=dic[ss[i]]
82+
return s
83+
84+
def check(ar):
85+
dic1={}
86+
for i in range(len(ar)):
87+
temp=morshword(ar[i])
88+
if temp in dic1:
89+
dic1[temp]+=1
90+
else:
91+
dic1[temp]=1
92+
return len(dic1)
93+
94+
for _ in range(int(input())):
95+
ar=input().split()
96+
print(check(ar))
97+
98+
99+
'''
100+
101+
########################### BY USING ARRAY AND ASCII VALUE ######################
102+
def morsh(ss):
103+
dic=[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
104+
s=""
105+
for i in ss:
106+
s += dic[ord(i)-97]
107+
return s
108+
def check(ar):
109+
dic1={}
110+
for i in range(len(ar)):
111+
temp=morsh(ar[i])
112+
if temp in dic1:
113+
dic1[temp]+=1
114+
else:
115+
dic1[temp]=1
116+
return len(dic1)
117+
118+
for _ in range(int(input())):
119+
ar=input().split()
120+
print(check(ar))

0 commit comments

Comments
 (0)