Skip to content

Commit 7f4f565

Browse files
BobTheBuilder44AnupKumarPanwar
authored andcommitted
subset_generation (TheAlgorithms#326)
* subset_generation generate all possible subset of size n of a given array of size r * Rename subset_generation to subset_generation.py * Update subset_generation.py I made all changes I could . What I mean is I removed all the empty space ....... There some comment extra if you feel removing those comments please do so yourself pls provide spacing as it should be * Create morse_Code_implementation.py * Any more changes pls let me know
1 parent 974088d commit 7f4f565

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

ciphers/morse_Code_implementation.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Python program to implement Morse Code Translator
2+
3+
4+
# Dictionary representing the morse code chart
5+
MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
6+
'C':'-.-.', 'D':'-..', 'E':'.',
7+
'F':'..-.', 'G':'--.', 'H':'....',
8+
'I':'..', 'J':'.---', 'K':'-.-',
9+
'L':'.-..', 'M':'--', 'N':'-.',
10+
'O':'---', 'P':'.--.', 'Q':'--.-',
11+
'R':'.-.', 'S':'...', 'T':'-',
12+
'U':'..-', 'V':'...-', 'W':'.--',
13+
'X':'-..-', 'Y':'-.--', 'Z':'--..',
14+
'1':'.----', '2':'..---', '3':'...--',
15+
'4':'....-', '5':'.....', '6':'-....',
16+
'7':'--...', '8':'---..', '9':'----.',
17+
'0':'-----', ', ':'--..--', '.':'.-.-.-',
18+
'?':'..--..', '/':'-..-.', '-':'-....-',
19+
'(':'-.--.', ')':'-.--.-'}
20+
21+
22+
def encrypt(message):
23+
cipher = ''
24+
for letter in message:
25+
if letter != ' ':
26+
27+
28+
cipher += MORSE_CODE_DICT[letter] + ' '
29+
else:
30+
31+
cipher += ' '
32+
33+
return cipher
34+
35+
36+
def decrypt(message):
37+
38+
message += ' '
39+
40+
decipher = ''
41+
citext = ''
42+
for letter in message:
43+
44+
if (letter != ' '):
45+
46+
47+
i = 0
48+
49+
50+
citext += letter
51+
52+
else:
53+
54+
i += 1
55+
56+
57+
if i == 2 :
58+
59+
60+
decipher += ' '
61+
else:
62+
63+
64+
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
65+
.values()).index(citext)]
66+
citext = ''
67+
68+
return decipher
69+
70+
71+
def main():
72+
message = "Morse code here"
73+
result = encrypt(message.upper())
74+
print (result)
75+
76+
message = result
77+
result = decrypt(message)
78+
print (result)
79+
80+
81+
if __name__ == '__main__':
82+
main()
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# python program to print all subset combination of n element in given set of r element .
2+
#arr[] ---> Input Array
3+
#data[] ---> Temporary array to store current combination
4+
# start & end ---> Staring and Ending indexes in arr[]
5+
# index ---> Current index in data[]
6+
#r ---> Size of a combination to be printed
7+
def combinationUtil(arr,n,r,index,data,i):
8+
#Current combination is ready to be printed,
9+
# print it
10+
if(index == r):
11+
for j in range(r):
12+
print(data[j],end =" ")
13+
print(" ")
14+
return
15+
# When no more elements are there to put in data[]
16+
if(i >= n):
17+
return
18+
#current is included, put next at next
19+
# location
20+
data[index] = arr[i]
21+
combinationUtil(arr,n,r,index+1,data,i+1)
22+
# current is excluded, replace it with
23+
# next (Note that i+1 is passed, but
24+
# index is not changed)
25+
combinationUtil(arr,n,r,index,data,i+1)
26+
# The main function that prints all combinations
27+
#of size r in arr[] of size n. This function
28+
#mainly uses combinationUtil()
29+
def printcombination(arr,n,r):
30+
# A temporary array to store all combination
31+
# one by one
32+
data = [0]*r
33+
#Print all combination using temprary
34+
#array 'data[]'
35+
combinationUtil(arr,n,r,0,data,0)
36+
# Driver function to check for above function
37+
arr = [10,20,30,40,50]
38+
printcombination(arr,len(arr),3)
39+
#This code is contributed by Ambuj sahu

0 commit comments

Comments
 (0)