-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathgetEncryptedNumber.py
More file actions
53 lines (43 loc) · 1.33 KB
/
getEncryptedNumber.py
File metadata and controls
53 lines (43 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Approach 1 (Python)
def getEncryptedNumber(numbers):
temp_storage=[]
if len(numbers)<=1:
return(numbers[0])
while len(numbers)!=2:
for item in range(len(numbers)-1):
new_item=numbers[item]+numbers[item+1]
if new_item>9:
new_item=new_item%10
temp_storage.append(new_item)
numbers=temp_storage
temp_storage=[]
return(str(numbers[0])+str(numbers[1]))
# Approach 2 (Python)
def Encryption(text):
while(len(text)>2):
temp = ''
for i in range(len(text) - 1):
if (int(text[i]) + int(text[i+1])) > 9:
temp += str( int(text[i]) + int(text[i+1]) - 10)
else:
temp += str( int(text[i]) + int(text[i+1]))
text = temp
return text
## Define main function ##
#Enter user input
text = input("Enter a number to be encrypted: ")
#calling and printing the function.
print('Encrypted number:',Encryption(text))
# Approach 3 (C++)
string getEncryptionNumber(int a[], int n){
// n-2th Pascal triangle line
int prev=1, t=n-2;
int f=a[0],e=a[n-1];
for(int i=1;i<=t;i++){
int curr = (prev*(t-i+1))/i;
prev=curr;
f+=(a[i]*curr);
e+=(a[n-i-1]*curr);
}
return to_string(f%10)+to_string(e%10);
}