Last active
April 13, 2021 01:46
-
-
Save rayanfer32/8487cbdf8e38cb754308217284acf696 to your computer and use it in GitHub Desktop.
INFOSYS Python Practice solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#DSA-Assgn-1 | |
def merge_list(list1, list2): | |
merged_data="" | |
list3=list() | |
#write your logic here | |
list2.reverse() | |
for i in range((len(list2))): | |
if list2[i] is not None: | |
merged_data+="%s%s "%(list1[i],list2[i]) | |
else: | |
merged_data+="%s "%(list1[i]) | |
resultant_data=merged_data | |
return resultant_data | |
#Provide different values for the variables and test your program | |
list1=['A', 'app','a', 'd', 'ke', 'th', 'doc', 'awa'] | |
list2=['y','tor','e','eps','ay',None,'le','n'] | |
merged_data=merge_list(list1,list2) | |
print(merged_data) | |
#DSA-Assgn-2 | |
class Car: | |
def __init__(self,model,year,registration_number): | |
self.__model=model | |
self.__year=year | |
self.__registration_number=registration_number | |
def get_model(self): | |
return self.__model | |
def get_year(self): | |
return self.__year | |
def get_registration_number(self): | |
return self.__registration_number | |
def __str__(self): | |
return(self.__model+" "+self.__registration_number+" "+(str)(self.__year)) | |
#Implement Service class here | |
class Service: | |
def __init__(self,car_list): | |
self.car_list=car_list | |
def get_car_list(self): | |
return self.car_list | |
def find_cars_by_year(self,year): | |
cars_in_year=[] | |
for car in self.car_list: | |
if(car.get_year()==year): | |
cars_in_year.append(car) | |
return cars_in_year | |
def add_cars(self,new_car_list): | |
self.car_list.extend(new_car_list) | |
return self.car_list | |
def remove_cars_from_karnataka(self): | |
for car in self.car_list: | |
if(car.get_registration_number().find("KA")): | |
print(car) | |
self.car_list.remove(car) | |
return self.car_list | |
car1=Car("WagonR",2010,"KA09 3056") | |
car2=Car("Beat", 2011, "MH10 6776") | |
car3=Car("Ritz", 2013,"KA12 9098") | |
car4=Car("Polo",2013,"GJ01 7854") | |
car5=Car("Amaze",2014,"KL07 4332") | |
#Add different values to the list and test the program | |
car_list=[car1, car2, car3, car4,car5] | |
#Create object of Service class, invoke the methods and test your program | |
ser=Service(car_list) | |
cl=ser.get_car_list() | |
for car in cl: | |
print(car) | |
print("---------------------------") | |
cy=ser.find_cars_by_year(2013) | |
for car in cy: | |
print(car) | |
car4=Car("afadf",2017,"KA01 7854") | |
car5=Car("dsasada",2017,"KL07 4332") | |
print("---------------------------") | |
sd=ser.add_cars([car4,car5]) | |
for car in sd: | |
print(car) | |
print("---------------------------") | |
sa=ser.remove_cars_from_karnataka() | |
for car in sa: | |
#print(car) | |
pass | |
#PF-Prac-2 | |
def bracket_pattern(input_str): | |
opening='(' | |
closing=')' | |
pattern=0 | |
for bracket in input_str: | |
if input_str[0] is closing or input_str[-1:] is opening: | |
return False | |
if bracket is opening: | |
pattern+=1 | |
else: | |
pattern-=1 | |
if(pattern%2==0): | |
return True | |
else: | |
return False | |
input_str="(())()" | |
print(bracket_pattern(input_str)) | |
#PF-Prac-4 | |
def find_nine(nums): | |
#Remove pass and write your code here | |
nums4=nums[:4] | |
if 9 in nums4: | |
return True | |
else: | |
return False | |
nums=[1,3,4,5,6] | |
print(find_nine(nums)) | |
#PF-Prac-5 | |
def count_digits_letters(sentence): | |
#start writing your code here | |
letters=0 | |
nums=0 | |
for i in sentence: | |
if(i.isdigit()): | |
nums+=1 | |
else: | |
letters+=1 | |
result_list=[letters,nums] | |
return result_list | |
sentence="Infosys Mysore 570027" | |
print(count_digits_letters(sentence)) | |
#PF-Prac-6 | |
def list123(nums): | |
#start writing your code here | |
seq=False | |
for i in range(len(nums)-2): | |
if(nums[i]==1 and nums[i+1]==2 and nums[i+2]==3): | |
return True | |
return False | |
nums=[1,1,1,2,3] | |
print(list123(nums)) | |
#PF-Prac-7 | |
def seed_no(number,ref_no): | |
#start writing your code here | |
num1=number | |
digits=[] | |
while(num1>0): | |
digits.append(num1%10) | |
num1=num1//10 | |
prod=number | |
for i in digits: | |
prod=prod*i | |
if(prod==ref_no): | |
return True | |
else: | |
return False | |
number=123 | |
ref_no=728 | |
print(seed_no(number,ref_no)) | |
#PF-Prac-8 | |
def calculate_net_amount(trans_list): | |
#start writing your code here | |
net_amount=0 | |
for transaction in trans_list: | |
action=transaction[0] | |
amount=int(transaction[2:]) | |
if(action=='D'): | |
net_amount+=amount | |
else: | |
net_amount-=amount | |
return net_amount | |
trans_list=["D:330","D:200","W:230","D:100"] | |
print(calculate_net_amount(trans_list)) | |
#PF-Prac-9 | |
def generate_dict(number): | |
#start writing your code here | |
new_dict={} | |
for num in range(1,number): | |
new_dict[num]=num**2 | |
return new_dict | |
number=20 | |
print(generate_dict(number)) | |
#PF-Prac-10 | |
def string_both_ends(input_string): | |
#start writing your code here | |
if(len(input_string)>1): | |
start=input_string[:2] | |
end=input_string[-2:] | |
return start+end | |
else: | |
return -1 | |
input_string="w3" | |
print(string_both_ends(input_string)) | |
#PF-Prac-11 | |
def find_upper_and_lower(sentence): | |
#start writing your code here | |
caps=small=0 | |
for i in sentence: | |
if(i.isupper()): | |
caps+=1 | |
else: | |
small+=1 | |
result_list=[caps,small] | |
return result_list | |
sentence="Come Here" | |
print(find_upper_and_lower(sentence)) | |
#PF-Prac-12 | |
def generate_sentences(subjects,verbs,objects): | |
sentence_list=[] | |
#start writing your code here | |
for subj in subjects: | |
for verb in verbs: | |
for obj in objects: | |
sentence=subj+" "+verb+" "+obj | |
sentence_list.append(sentence) | |
return sentence_list | |
subjects=["I","You"] | |
verbs=["love", "play"] | |
objects=["Hockey","Football"] | |
print(generate_sentences(subjects,verbs,objects)) | |
#PF-Prac-13 | |
def close_number(num1,num2,num3): | |
flagA=flagB=False | |
#start writing your code here | |
diffs=[] | |
#cond A to check numbers close by atmost 1 | |
ndiff=[num1-num2,num2-num3,num3-num1] | |
for i in ndiff: | |
if(i<=0): | |
diffs.append(i*-1) | |
else: | |
diffs.append(i) | |
if(min(diffs)<=1): | |
flagA=True | |
#find missing num | |
print(diffs) | |
#cond B to check missed number>2 | |
diffs.remove(1) | |
if(max(diffs)>2): | |
flagB=True | |
if (flagA and flagB): | |
return True | |
else: | |
return False | |
print(close_number(1,2,3)) | |
#PF-Prac-16 | |
def rotate_list(input_list,n): | |
#start writing your code here | |
output_list=[] | |
if(n==0) | |
return input_list | |
readPos=len(input_list)-n | |
for i in input_list: | |
output_list.append(input_list[readPos]) | |
readPos+=1 | |
if(readPos==len(input_list)): | |
readPos=0 | |
return output_list | |
input_list= [1,2,3,4,5,6] | |
output_list=rotate_list(input_list,1) | |
print(output_list) | |
#PF-Prac-32 | |
#import math | |
def check_squares(number): | |
#start writing your code here | |
limit=int(number**0.5)+1 | |
print(limit) | |
for i in range(1,limit): | |
for j in range(1,limit): | |
res=(i**2)+(j**2) | |
if(res==number): | |
return True | |
return False | |
number=13 | |
print(check_squares(number)) | |
#PF-Prac-33 | |
def integer_to_english(number): | |
#start writing your code here | |
subNum="" | |
inEnglish="" | |
numWord={0:"\0",1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine",10:"ten",11:"eleven",12:"twelve",13:"thirteen",14:"fourteen",15:"fifteen",16:"sixteen",17:"seventeen",18:"eighteen",19:"nineteen",20:"twenty",30:"thirty",40:"fourty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninty",100:"hundred",1000:"thousand"} | |
if(number>1000): | |
return -1 | |
else: | |
if(number in numWord.keys()): | |
inEnglish=numWord[number] | |
return inEnglish | |
else: | |
subNum=str(number) | |
if(number>99): | |
hundreds=int(subNum[0]) | |
tens=int(subNum[1])*10 | |
units=int(subNum[2]) | |
if(number<=99): | |
tens=int(subNum[0])*10 | |
units=int(subNum[1]) | |
if(number<100): | |
inEnglish=numWord[tens]+" "+numWord[units] | |
else: | |
if(number<=120): | |
tens=tens+units | |
inEnglish=numWord[hundreds]+" "+"hundred and "+numWord[tens] | |
else: | |
inEnglish=numWord[hundreds]+" "+"hundred and "+numWord[tens]+" "+numWord[units] | |
return inEnglish | |
number=140 | |
print(integer_to_english(number)+" only!") | |
"""Consider a non-empty array of comma separated strings instr, where each element is of the format alphabets:number, return the string outstr after performing the below operations for each element: | |
Find sum of squares of the digits present in number | |
If the sum is even, then rotate the corresponding alphabets once towards right | |
If the sum is odd, rotate the corresponding alphabets twice towards left | |
Concatenate the rotated alphabets to the output string outstr separated by ','(comma) | |
Assumption: number will always be non zero positive integer and alphabets will contain at least one alphabet | |
Input format: | |
Read the array of strings instr with the elements separated by ','(comma) from the standard input stream. | |
Output format: | |
Print the string outstr to the standard output stream. | |
[Sample Input]: | |
rtAG:10328,nsHDE:85637 | |
[Sample Output ]: | |
GrtA,HDEns | |
[EXPLANATION]: | |
The first string is ‘rtAG:10328’, the sum of square of the digits 1, 0, 3, 2 and 8 is 78 which is even. So the alphabets after rotating once towards the right is 'GrtA'. The second string is 'nsHDE:85637', the sum of square of the digits 8, 5, 6, 3 and 7 is 183 which is odd. So the alphabets after rotating twice towards the left is 'HDEns'. Hence the output is 'GrtA,HDEns' | |
[SOLUTION] | |
""" | |
instr="rtAG:10328,nsHDE:85637" | |
strs=instr.split(',') | |
outlist=[] | |
def rotright1(code): | |
return code[-1]+code[:-1] | |
def rotleft2(code): | |
return code[2:]+code[:2] | |
for msg in strs: | |
code,value=msg.split(":") | |
squares=list(str(value)) | |
sqsum=0 | |
sqsum=sum([int(i)**2 for i in squares]) | |
if(sqsum%2 == 0): | |
outlist.append(rotright1(code)) | |
else: | |
outlist.append(rotleft2(code)) | |
outstr=",".join(outlist) | |
print(outstr) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Learn and Practice👍