Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeeksForGeeks interview guide: Adding pythagorean triplet problem and roman to integer conversion #58 #99

Merged
merged 2 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions GeeksforGeeks/pythagorean_triplet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''Given an array arr of N integers
write a function that
returns true if there is a triplet (a, b, c)
that satisfies a2 + b2 = c2
otherwise false'''

import math

def checkTriplet(arr, n):
mxm = 0

# finding maximum element
for element in arr:
mxm = max(mxm, element)

# hashing array
hash = [0]*(mxm+1)

for element in arr:
hash[element] += 1

for i in range(1, mxm+1):
if hash[i] == 0:
continue

for j in range(1, mxm+1):
if (i==j and hash[i]==1) or hash[j]==0:
continue

val = int(math.sqrt(i*i+j*j))
if val*val != (i*i+j*j):
continue
if val>mxm:
continue
if hash[val]:
return True
return False

'''n = int(input())
a = list(map(int,input().split()))'''

N = 5
Arr = [3, 2, 4, 6, 5]

# N = 3
# Arr = [3, 8, 5]

if checkTriplet(Arr, N):
print("Yes")
else:
print("No")
41 changes: 41 additions & 0 deletions GeeksforGeeks/roman_to_integer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''
Input:
The first line of each test case contains the no of test cases T
Then T test cases follow. Each test case contains a string s denoting the roman no

Output:
For each test case in a new line print the integer representation of roman number s
'''

def roman_to_int(roman):
value = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}

prev = 0
ans = 0
n = len(roman)
for i in range(n-1, -1, -1):
if value[roman[i]] >= prev:
ans += value[roman[i]]
else:
ans -= value[roman[i]]

prev = value[roman[i]]

print(ans)
'''for _ in range(int(input())):
s = input()
roman_to_int(s)
'''
# s = 'V'
# s = 'III'
s = 'XIV'

roman_to_int(s)