├── Data_Structures ├── Assignment_set_1 │ ├── Assignment-1.py │ ├── Assignment-2.py │ ├── Assignment-3.py │ └── Assignment-4.py ├── Assignment_set_2 │ └── Assignment-1.py ├── Assignment_set_3 │ ├── Assignment-1.py │ ├── Assignment-2.py │ ├── Assignment-3.py │ ├── Assignment-4.py │ └── Assignment-5.py └── Assignment_set_4 │ ├── Assignment-1.py │ ├── Assignment-2.py │ ├── Assignment-3.py │ └── Assignment-4.py ├── Fundamental Programming Part 1 ├── Assignment 1 │ ├── (O>)To(E) │ ├── (O>)To(E)-2 │ ├── Armstrong Number │ ├── CanvasProblem1 │ ├── CanvasProblem2 │ ├── Leap Year Problem │ ├── Maze Problem │ ├── Minimum of 3 Numbers │ ├── Palindrome Problem │ ├── Scholarship Problem │ └── SumDivisibleBy4 ├── Assignment 2 │ ├── ProductOfThreeNumber.py │ ├── SideOfTriangle.py │ ├── calculate_bill_amount.py │ ├── calculate_loan.py │ ├── count_names.py │ ├── find_max.py │ ├── generate_ticket.py │ ├── get_count.py │ ├── make_amount.py │ └── solve.py ├── Assignment 6 │ ├── 10-SubstringOfaNumber.py │ ├── Find_Smallest_Number.py │ ├── Find_duplicates.py │ ├── Palindrome_String.py │ └── SumOfPrimeFactors.py ├── Assignment-5_(Day-5) │ ├── Even_Odd_Sum.py │ ├── Student_report.py │ ├── calculate_profit-loss.py │ ├── check_double.py │ └── find_pairs_of_numbers.py ├── Assignment-7(Day-7) │ ├── Check_Anagram.py │ ├── Nearest_Palindrome.py │ ├── Perfect_Number.py │ ├── Remove_Duplicates.py │ ├── Ticket_List.py │ ├── ValidateCreditCardNumber.py │ ├── Validate_all.py │ └── get_circular_prime_count.py ├── Assignment_Set-3 │ ├── calculate_bill_amount.py │ ├── calculate_total_chocolates.py │ ├── check_palindrome.py │ ├── create_largest_number.py │ ├── encode.py │ ├── find_leap_years.py │ └── translate.py └── Assignment_Set-4 │ ├── encrypt_sentence.py │ ├── find_common_characters.py │ ├── find_correct.py │ ├── max_frequency_word_counter.py │ ├── max_visited_speciality.py │ └── sms_encoding.py └── README.md /Data_Structures/Assignment_set_1/Assignment-1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Given two lists, both having String elements, write a python program using python lists to create a new string as per the rule given below: 4 | The first element in list1 should be merged with last element in list2, second element in list1 should be merged with second last element 5 | in list2 and so on. If an element in list1/list2 is None, then the corresponding element in the other list should be kept as it is in the merged list. 6 | 7 | Sample Input Expected Output 8 | list1=['A', 'app','a', 'd', 'ke', 'th', 'doc', 'awa'] 9 | 10 | list2=['y','tor','e','eps','ay',None,'le','n'] 11 | 12 | "An apple a day keeps the doctor away" 13 | 14 | Solution: 15 | ''' 16 | 17 | #lex_auth_0127426166978887681375 18 | 19 | def merge_list(list1, list2): 20 | merged_data="" 21 | list2.reverse() 22 | if len(list1)!=len(list2): 23 | if len(list1)>len(list2): 24 | length=len(list1) 25 | else: 26 | length=len(list2) 27 | else: 28 | length=len(list1) 29 | for i in range(0,length): 30 | if list1[i]==None: 31 | merged_data+=list2[i] 32 | elif list2[i]==None: 33 | merged_data+=list1[i] 34 | else: 35 | merged_data+=list1[i] + list2[i] 36 | 37 | if i!=length-1: 38 | merged_data+=" " 39 | return merged_data 40 | 41 | #Provide different values for the variables and test your program 42 | list1=['A', 'app','a', 'd', 'ke', 'th', 'doc', 'awa'] 43 | list2=['y','tor','e','eps','ay',None,'le','n'] 44 | merged_data=merge_list(list1,list2) 45 | print(merged_data) 46 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_1/Assignment-2.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Consider a Car class as given in the code. Write a Service class as given in the class diagram below which performs various activities on 4 | a list of cars. 5 | Assume that the car_list is sorted by year in ascending order. 6 | 7 | Method Description:-- 8 | __init__(car_list): Initializes the instance variable, car_list. 9 | find_cars_by_year(year): Finds and returns the list of models of all the cars with the year as the one passed as the argument. 10 | If there are no cars, return None. 11 | add_cars(new_car_list): The new_car_list should be added to the instance variable car_list. The car_list should be still sorted 12 | such that the years are in ascending order. 13 | remove_cars_from_karnataka(): Finds and removes all cars with registration number beginning with “KA” from the car_list. 14 | 15 | Solution: 16 | ''' 17 | 18 | #lex_auth_0127426245709905921377 19 | 20 | class Car: 21 | def __init__(self,model,year,registration_number): 22 | self.__model=model 23 | self.__year=year 24 | self.__registration_number=registration_number 25 | 26 | def get_model(self): 27 | return self.__model 28 | 29 | def get_year(self): 30 | return self.__year 31 | 32 | def get_registration_number(self): 33 | return self.__registration_number 34 | 35 | def __str__(self): 36 | return(self.__model+" "+self.__registration_number+" "+(str)(self.__year)) 37 | 38 | class Service: 39 | def __init__(self,car_list): 40 | self.__car_list=car_list 41 | 42 | def get_car_list(self): 43 | return self.__car_list 44 | 45 | def find_cars_by_year(self,year): 46 | list=[] 47 | flag=0 48 | for item in self.__car_list: 49 | if item.get_year()==year: 50 | list.append(item.get_model()) 51 | flag=1 52 | if flag==1: 53 | return list 54 | else: 55 | return None 56 | 57 | def add_cars(self,new_car_list): 58 | for item in new_car_list: 59 | self.__car_list.append(item) 60 | 61 | list=[] 62 | for item in self.__car_list: 63 | list.append(item.get_year()) 64 | 65 | list.sort() 66 | 67 | list1=[] 68 | for item1 in list: 69 | for item2 in self.__car_list: 70 | if item2.get_year()==item1 and item2 not in list1: 71 | list1.append(item2) 72 | break 73 | self.__car_list=list1 74 | 75 | def remove_cars_from_karnataka(self): 76 | list=[] 77 | for item in self.__car_list: 78 | if item.get_registration_number()[0:2]=="KA": 79 | list.append(item) 80 | 81 | for item1 in list: 82 | for item2 in self.__car_list: 83 | if item1==item2: 84 | self.__car_list.remove(item1) 85 | 86 | car1=Car("Beat", 2011, "KA12 6776") 87 | car2=Car("WagonR",2010,"KA09 3056") 88 | car3=Car("Ritz", 2013,"MH10 9098") 89 | car4=Car("Polo",2013,"GJ01 7854") 90 | car5=Car("Amaze",2014,"KL07 4332") 91 | #Add different values to the list and test the program 92 | car_list=[car1, car2, car3, car4,car5] 93 | Service(car_list).remove_cars_from_karnataka() 94 | #Create object of Service class, invoke the methods and test your program 95 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_1/Assignment-3.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Given two lists, both having integer elements, write a python program using python lists to create and return a new list as per the rule given below: 4 | 5 | If the double of an element in list1 is present in list2, then add it to the new list. 6 | 7 | 8 | Sample Input Expected Output 9 | list1 - [11, 8,23,7,25, 15] 10 | list2 – [6, 33, 50,31, 46, 78, 16,34] new_list – [8,23,25] 11 | 12 | Solutions: 13 | 14 | ''' 15 | 16 | #lex_auth_0127426336682147841449 17 | 18 | def check_double(list1,list2): 19 | new_list=[] 20 | 21 | for item1 in list1: 22 | for item2 in list2: 23 | if item2==2*item1 and item1 not in new_list: 24 | new_list.append(item1) 25 | return new_list 26 | 27 | #Provide different values for the variables and test your program 28 | list1=[11,8,23,7,25,15] 29 | list2=[6,33,50,31,46,78,16,34] 30 | print(check_double(list1, list2)) 31 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_1/Assignment-4.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | The Indian Cricket Board has decided to send its best players to ICC World Cup. They have chosen the best 11 players for the match. 4 | The player’s names and their experiences in years are as given below. 5 | 6 | Name Dhoni Virat Rohit Raina Jadeja Ishant Shikhar Axar Ashwin Stuart Bhuvneshwar 7 | Experience 15 10 12 11 13 9 8 7.5 6 7 5 8 | The players should be sent in for the batting based on their experience, i.e. the priority would be given to the player who has got the 9 | highest experience. At the last moment, the captain has decided that the player who has got the highest experience should bat at the 4th 10 | position. 11 | 12 | Use the Player class and players_list provided to implement the class Game as given in the class diagram. 13 | 14 | 1. In the constructor of Game class, initialize players_list with list of players 15 | 16 | 2. Display the name and experience of the players after sorting the players based on their experience(descending order) 17 | 18 | 3. Display the name and experience of the players after moving the first player (0 index position) to the 4th (3 index position) position. 19 | 20 | Solution: 21 | 22 | ''' 23 | #DSA-Assgn-4 24 | 25 | class Player: 26 | def __init__(self,name,experience): 27 | self.__name=name 28 | self.__experience=experience 29 | 30 | def get_name(self): 31 | return self.__name 32 | 33 | def get_experience(self): 34 | return self.__experience 35 | 36 | def __str__(self): 37 | return(self.__name+" "+(str)(self.__experience)) 38 | 39 | class Game: 40 | def __init__(self,players_list): 41 | self.__players_list=players_list 42 | 43 | def sort_players_based_on_experience(self): 44 | list2=[] 45 | for item in self.__players_list: 46 | list2.append(item.get_experience()) 47 | list2.sort() 48 | list2.reverse() 49 | 50 | list1=[] 51 | for item1 in list2: 52 | for item2 in self.__players_list: 53 | if item1==item2.get_experience() and item2 not in list1: 54 | list1.append(item2) 55 | break 56 | self.__players_list=list1 57 | def shift_player_to_new_position(self, 58 | old_index_position,new_index_position): 59 | if old_index_positionnew_index_position: 67 | temp=self.__players_list[new_index_position] 68 | self.__players_list.remove( 69 | self.__players_list[new_index_position]) 70 | self.__players_list.insert( 71 | new_index_position, 72 | self.__players_list[old_index_position-1]) 73 | self.__players_list.remove( 74 | self.__players_list[old_index_position]) 75 | self.__players_list.insert(old_index_position,temp) 76 | 77 | def display_player_details(self): 78 | for item in self.__players_list: 79 | print(item) 80 | 81 | player1=Player("Dhoni",15) 82 | player2=Player("Virat",10) 83 | player3=Player("Rohit",12) 84 | player4=Player("Raina",11) 85 | player5=Player("Jadeja",13) 86 | player6=Player("Ishant",9) 87 | player7=Player("Shikhar",8) 88 | player8=Player("Axar",7.5) 89 | player9=Player("Ashwin",6) 90 | player10=Player("Stuart",7) 91 | player11=Player("Bhuvneshwar",5) 92 | #Add different values to the list and test the program 93 | players_list=[player1,player2,player3,player4,player5,player6,player7,player8,player9,player10,player11] 94 | #Create object of Game class, invoke the methods and test your program 95 | G1=Game(players_list) 96 | G1.display_player_details() 97 | G1.shift_player_to_new_position(2, 5) 98 | print("\n\n") 99 | G1.display_player_details() 100 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_2/Assignment-1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Given a linked list of characters. Write a python function to return a new string that is created by appending all the characters given in 4 | the linked list as per the rules given below. 5 | 6 | Rules: 7 | 8 | 1. Replace '*' or '/' by a single space 9 | 10 | 2. In case of two consecutive occurrences of '*' or '/' , replace those two occurrences by a single space and convert the next character 11 | to upper case. 12 | 13 | Assume that 14 | 15 | 1. There will not be more than two consecutive occurrences of '*' or '/' 16 | 17 | 2. The linked list will always end with an alphabet 18 | 19 | Sample Input Expected Output 20 | A,n,*,/,a,p,p,l,e,*,a,/,day,*,*,k,e,e,p,s,/,*,a,/,/,d,o,c,t,o,r,*,A,w,a,y An Apple a day Keeps A Doctor Away 21 | 22 | Solution: 23 | ''' 24 | #lex_auth_0127438603766333441613 25 | 26 | class Node: 27 | def __init__(self,data): 28 | self.__data=data 29 | self.__next=None 30 | 31 | def get_data(self): 32 | return self.__data 33 | 34 | def set_data(self,data): 35 | self.__data=data 36 | 37 | def get_next(self): 38 | return self.__next 39 | 40 | def set_next(self,next_node): 41 | self.__next=next_node 42 | 43 | 44 | class LinkedList: 45 | def __init__(self): 46 | self.__head=None 47 | self.__tail=None 48 | 49 | def get_head(self): 50 | return self.__head 51 | 52 | def get_tail(self): 53 | return self.__tail 54 | 55 | 56 | def add(self,data): 57 | new_node=Node(data) 58 | if(self.__head is None): 59 | self.__head=self.__tail=new_node 60 | else: 61 | self.__tail.set_next(new_node) 62 | self.__tail=new_node 63 | 64 | def insert(self,data,data_before): 65 | new_node=Node(data) 66 | if(data_before==None): 67 | new_node.set_next(self.__head) 68 | self.__head=new_node 69 | if(new_node.get_next()==None): 70 | self.__tail=new_node 71 | 72 | else: 73 | node_before=self.find_node(data_before) 74 | if(node_before is not None): 75 | new_node.set_next(node_before.get_next()) 76 | node_before.set_next(new_node) 77 | if(new_node.get_next() is None): 78 | self.__tail=new_node 79 | else: 80 | print(data_before,"is not present in the Linked list") 81 | 82 | def display(self): 83 | temp=self.__head 84 | while(temp is not None): 85 | print(temp.get_data()) 86 | temp=temp.get_next() 87 | 88 | 89 | def find_node(self,data): 90 | temp=self.__head 91 | while(temp is not None): 92 | if(temp.get_data()==data): 93 | return temp 94 | temp=temp.get_next() 95 | return None 96 | 97 | def delete(self,data): 98 | node=self.find_node(data) 99 | if(node is not None): 100 | if(node==self.__head): 101 | if(self.__head==self.__tail): 102 | self.__tail=None 103 | self.__head=node.get_next() 104 | else: 105 | temp=self.__head 106 | while(temp is not None): 107 | if(temp.get_next()==node): 108 | temp.set_next(node.get_next()) 109 | if(node==self.__tail): 110 | self.__tail=temp 111 | node.set_next(None) 112 | break 113 | temp=temp.get_next() 114 | else: 115 | print(data,"is not present in Linked list") 116 | 117 | #You can use the below __str__() to print the elements of the DS object while debugging 118 | def __str__(self): 119 | temp=self.__head 120 | msg=[] 121 | while(temp is not None): 122 | msg.append(str(temp.get_data())) 123 | temp=temp.get_next() 124 | msg=" ".join(msg) 125 | msg="Linkedlist data(Head to Tail): "+ msg 126 | return msg 127 | 128 | 129 | def create_new_sentence(word_list): 130 | new_sentence="" 131 | temp=word_list.get_head() 132 | count=0 133 | ''' 134 | while temp!=None: 135 | if temp.get_data() not in ['*','/']: 136 | new_sentence+=temp.get_data 137 | 138 | elif temp.get_data() in['*','/']: 139 | 140 | 141 | 142 | ''' 143 | flag=0 144 | while(temp!=None): 145 | if temp.get_data() not in ['*','/'] and flag==0: 146 | new_sentence+=temp.get_data() 147 | temp=temp.get_next() 148 | elif temp.get_data() in ['*','/']: 149 | if temp.get_next().get_data() not in ['*','/']: 150 | new_sentence+=" " 151 | temp=temp.get_next() 152 | elif temp.get_next().get_data() in ['*','/']: 153 | new_sentence+=" " 154 | temp=(temp.get_next()).get_next() 155 | flag=1 156 | elif temp.get_data() not in ['*','/'] and flag==1: 157 | new_sentence+=(temp.get_data()).upper() 158 | flag=0 159 | temp=temp.get_next() 160 | 161 | return new_sentence 162 | 163 | word_list=LinkedList() 164 | word_list.add("T") 165 | word_list.add("h") 166 | word_list.add("e") 167 | word_list.add("/") 168 | word_list.add("*") 169 | word_list.add("s") 170 | word_list.add("k") 171 | word_list.add("y") 172 | word_list.add("*") 173 | word_list.add("i") 174 | word_list.add("s") 175 | word_list.add("/") 176 | word_list.add("/") 177 | word_list.add("b") 178 | word_list.add("l") 179 | word_list.add("u") 180 | word_list.add("e") 181 | result=create_new_sentence(word_list) 182 | print(result) 183 | 184 | 185 | 186 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_3/Assignment-1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Given two queues, one integer queue and another character queue, write a python program to merge them to form a single queue. 4 | Follow the below rules for merging: 5 | Merge elements at the same position starting with the integer queue. 6 | If one of the queues has more elements than the other, add all the additional elements at the end of the output queue. 7 | Note : max_size of the merged queue should be the sum of the size of both the queues. 8 | For example, 9 | Input -- queue1: 3,6,8 queue2: b,y,u,t,r,o 10 | Output -- 3,b,6,y,8,u,t,r,o 11 | Solution: 12 | ''' 13 | ''' 14 | Created on Oct 20, 2019 15 | @author: Sanjeev K. Mishra 16 | ''' 17 | #lex_auth_0127438930044108801627 18 | 19 | class Queue: 20 | def __init__(self,max_size): 21 | 22 | self.__max_size=max_size 23 | self.__elements=[None]*self.__max_size 24 | self.__rear=-1 25 | self.__front=0 26 | 27 | def is_full(self): 28 | if(self.__rear==self.__max_size-1): 29 | return True 30 | return False 31 | 32 | def is_empty(self): 33 | if(self.__front>self.__rear): 34 | return True 35 | return False 36 | 37 | def enqueue(self,data): 38 | if(self.is_full()): 39 | print("Queue is full!!!") 40 | else: 41 | self.__rear+=1 42 | self.__elements[self.__rear]=data 43 | 44 | def dequeue(self): 45 | if(self.is_empty()): 46 | print("Queue is empty!!!") 47 | else: 48 | data=self.__elements[self.__front] 49 | self.__front+=1 50 | return data 51 | 52 | def display(self): 53 | for index in range(self.__front, self.__rear+1): 54 | print(self.__elements[index]) 55 | 56 | 57 | def get_max_size(self): 58 | return self.__max_size 59 | 60 | #You can use the below __str__() to print the elements of the DS object while debugging 61 | def __str__(self): 62 | msg=[] 63 | index=self.__front 64 | while(index<=self.__rear): 65 | msg.append((str)(self.__elements[index])) 66 | index+=1 67 | msg=" ".join(msg) 68 | msg="Queue data(Front to Rear): "+msg 69 | return msg 70 | 71 | def merge_queue(queue1,queue2): 72 | list1=[] 73 | list2=[] 74 | list3=[] 75 | 76 | while (not queue1.is_empty()): 77 | list1.append(queue1.dequeue()) 78 | 79 | while (not queue2.is_empty()): 80 | list2.append(queue2.dequeue()) 81 | 82 | check=0 83 | if len(list1)self.__rear): 38 | return True 39 | return False 40 | 41 | def enqueue(self,data): 42 | if(self.is_full()): 43 | print("Queue is full!!!") 44 | else: 45 | self.__rear+=1 46 | self.__elements[self.__rear]=data 47 | 48 | def dequeue(self): 49 | if(self.is_empty()): 50 | print("Queue is empty!!!") 51 | else: 52 | data=self.__elements[self.__front] 53 | self.__front+=1 54 | return data 55 | 56 | def display(self): 57 | for index in range(self.__front, self.__rear+1): 58 | print(self.__elements[index]) 59 | 60 | 61 | def get_max_size(self): 62 | return self.__max_size 63 | 64 | #You can use the below __str__() to print the elements of the DS object while debugging 65 | def __str__(self): 66 | msg=[] 67 | index=self.__front 68 | while(index<=self.__rear): 69 | msg.append((str)(self.__elements[index])) 70 | index+=1 71 | msg=" ".join(msg) 72 | msg="Queue data(Front to Rear): "+msg 73 | return msg 74 | 75 | class Stack: 76 | def __init__(self,max_size): 77 | self.__max_size=max_size 78 | self.__elements=[None]*self.__max_size 79 | self.__top=-1 80 | 81 | def is_full(self): 82 | if(self.__top==self.__max_size-1): 83 | return True 84 | return False 85 | 86 | def is_empty(self): 87 | if(self.__top==-1): 88 | return True 89 | return False 90 | 91 | def push(self,data): 92 | if(self.is_full()): 93 | print("The stack is full!!") 94 | else: 95 | self.__top+=1 96 | self.__elements[self.__top]=data 97 | 98 | def pop(self): 99 | if(self.is_empty()): 100 | print("The stack is empty!!") 101 | else: 102 | data= self.__elements[self.__top] 103 | self.__top-=1 104 | return data 105 | 106 | def display(self): 107 | if(self.is_empty()): 108 | print("The stack is empty") 109 | else: 110 | index=self.__top 111 | while(index>=0): 112 | print(self.__elements[index]) 113 | index-=1 114 | 115 | def get_max_size(self): 116 | return self.__max_size 117 | 118 | #You can use the below __str__() to print the elements of the DS object while debugging 119 | def __str__(self): 120 | msg=[] 121 | index=self.__top 122 | while(index>=0): 123 | msg.append((str)(self.__elements[index])) 124 | index-=1 125 | msg=" ".join(msg) 126 | msg="Stack data(Top to Bottom): "+msg 127 | return msg 128 | 129 | 130 | class Ball: 131 | def __init__(self,color,name): 132 | self.__color=color 133 | self.__name=name 134 | 135 | def __str__(self): 136 | return (self.__color+" "+self.__name) 137 | 138 | def get_color(self): 139 | return self.__color 140 | 141 | def get_name(self): 142 | return self.__name 143 | 144 | class Game: 145 | def __init__(self,ball_stack): 146 | self.ball_container=ball_stack 147 | self.red_balls_container=Stack(2) 148 | self.green_balls_container=Stack(2) 149 | self.blue_balls_container=Stack(2) 150 | self.yellow_balls_container=Stack(2) 151 | 152 | def grouping_based_on_color(self): 153 | while(not self.ball_container.is_empty()): 154 | temp=self.ball_container.pop() 155 | if temp.get_color()=='Red': 156 | self.red_balls_container.push(temp) 157 | elif temp.get_color()=='Blue': 158 | self.blue_balls_container.push(temp) 159 | elif temp.get_color()=='Yellow': 160 | self.yellow_balls_container.push(temp) 161 | elif temp.get_color()=='Green': 162 | self.green_balls_container.push(temp) 163 | 164 | def rearrange_balls(self,color): 165 | if color=='Red': 166 | 167 | e1=self.red_balls_container.pop() 168 | e2=self.red_balls_container.pop() 169 | if e1.get_name()=='A': 170 | self.red_balls_container.push(e2) 171 | self.red_balls_container.push(e1) 172 | else: 173 | self.red_balls_container.push(e1) 174 | self.red_balls_container.push(e2) 175 | 176 | elif color=='Blue': 177 | e1=self.blue_balls_container.pop() 178 | e2=self.blue_balls_container.pop() 179 | if e1.get_name()=='A': 180 | self.blue_balls_container.push(e2) 181 | self.blue_balls_container.push(e1) 182 | else: 183 | self.blue_balls_container.push(e1) 184 | self.blue_balls_container.push(e2) 185 | elif color=='Yellow': 186 | 187 | e1=self.yellow_balls_container.pop() 188 | e2=self.yellow_balls_container.pop() 189 | if e1.get_name()=='A': 190 | self.yellow_balls_container.push(e2) 191 | self.yellow_balls_container.push(e1) 192 | else: 193 | self.yellow_balls_container.push(e1) 194 | self.yellow_balls_container.push(e2) 195 | 196 | elif color=='Green': 197 | e1=self.green_balls_container.pop() 198 | e2=self.green_balls_container.pop() 199 | if e1.get_name()=='A': 200 | self.green_balls_container.push(e2) 201 | self.green_balls_container.push(e1) 202 | else: 203 | self.green_balls_container.push(e1) 204 | self.green_balls_container.push(e2) 205 | 206 | def display_ball_details(self,color): 207 | while(not self.ball_container.is_empty()): 208 | ball=self.ball_container.pop() 209 | print(ball.get_name(),ball.get_type()) 210 | 211 | if color=='Red': 212 | print(self.red_balls_container.pop().get_name(),self.red_balls_container.pop().get_type()) 213 | print(self.red_balls_container.pop().get_name(),self.red_balls_container.pop().get_type()) 214 | elif color=='Blue': 215 | print(self.blue_balls_container.pop().get_name(),self.blue_balls_container.pop().get_type()) 216 | print(self.blue_balls_container.pop().get_name(),self.blue_balls_container.pop().get_type()) 217 | elif color=='Yellow': 218 | print(self.yellow_balls_container.pop().get_name(),self.yellow_balls_container.pop().get_type()) 219 | print(self.yellow_balls_container.pop().get_name(),self.yellow_balls_container.pop().get_type()) 220 | elif color=='Green': 221 | print(self.green_balls_container.pop().get_name(),self.green_balls_container.pop().get_type()) 222 | print(self.green_balls_container.pop().get_name(),self.green_balls_container.pop().get_type()) 223 | 224 | 225 | #Use different values to test your program 226 | ball1=Ball("Red","A") 227 | ball2=Ball("Blue","B") 228 | ball3=Ball("Yellow","B") 229 | ball4=Ball("Blue","A") 230 | ball5=Ball("Yellow","A") 231 | ball6=Ball("Green","B") 232 | ball7=Ball("Green","A") 233 | ball8=Ball("Red","B") 234 | ball_list=Stack(8) 235 | ball_list.push(ball1) 236 | ball_list.push(ball2) 237 | ball_list.push(ball3) 238 | ball_list.push(ball4) 239 | ball_list.push(ball5) 240 | ball_list.push(ball6) 241 | ball_list.push(ball7) 242 | ball_list.push(ball8) 243 | Game(ball_list).grouping_based_on_color() 244 | Game(ball_list).display_ball_details('Yellow') 245 | #Create objects of Game class, invoke the methods and test the program 246 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_3/Assignment-3.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Given a stack of integers, write a python program that updates the input stack such that all occurrences of the smallest values are at the bottom of the stack, while the order of the other elements remains the same. 4 | For example: 5 | Input stack (top-bottom) : 5 66 5 8 7 6 | Output: 66 8 7 5 5 7 | Solution: 8 | ''' 9 | 10 | ''' 11 | Created on Oct 20, 2019 12 | @author: HITMAN 13 | ''' 14 | #lex_auth_0127438990347550721631 15 | 16 | class Stack: 17 | def __init__(self,max_size): 18 | self.__max_size=max_size 19 | self.__elements=[None]*self.__max_size 20 | self.__top=-1 21 | 22 | def is_full(self): 23 | if(self.__top==self.__max_size-1): 24 | return True 25 | return False 26 | 27 | def is_empty(self): 28 | if(self.__top==-1): 29 | return True 30 | return False 31 | 32 | def push(self,data): 33 | if(self.is_full()): 34 | print("The stack is full!!") 35 | else: 36 | self.__top+=1 37 | self.__elements[self.__top]=data 38 | 39 | def pop(self): 40 | if(self.is_empty()): 41 | print("The stack is empty!!") 42 | else: 43 | data= self.__elements[self.__top] 44 | self.__top-=1 45 | return data 46 | 47 | def display(self): 48 | if(self.is_empty()): 49 | print("The stack is empty") 50 | else: 51 | index=self.__top 52 | while(index>=0): 53 | print(self.__elements[index]) 54 | index-=1 55 | 56 | def get_max_size(self): 57 | return self.__max_size 58 | 59 | #You can use the below __str__() to print the elements of the DS object while debugging 60 | def __str__(self): 61 | msg=[] 62 | index=self.__top 63 | while(index>=0): 64 | msg.append((str)(self.__elements[index])) 65 | index-=1 66 | msg=" ".join(msg) 67 | msg="Stack data(Top to Bottom): "+msg 68 | return msg 69 | 70 | 71 | def change_smallest_value(number_stack): 72 | list1=[] 73 | 74 | while not number_stack.is_empty(): 75 | list1.append(number_stack.pop()) 76 | 77 | temp=list1 78 | 79 | min=99999 80 | for i in temp: 81 | if iself.__rear): 33 | return True 34 | return False 35 | 36 | def enqueue(self,data): 37 | if(self.is_full()): 38 | print("Queue is full!!!") 39 | else: 40 | self.__rear+=1 41 | self.__elements[self.__rear]=data 42 | 43 | def dequeue(self): 44 | if(self.is_empty()): 45 | print("Queue is empty!!!") 46 | else: 47 | data=self.__elements[self.__front] 48 | self.__front+=1 49 | return data 50 | 51 | def display(self): 52 | for index in range(self.__front, self.__rear+1): 53 | print(self.__elements[index]) 54 | 55 | 56 | def get_max_size(self): 57 | return self.__max_size 58 | 59 | #You can use the below __str__() to print the elements of the DS object while debugging 60 | def __str__(self): 61 | msg=[] 62 | index=self.__front 63 | while(index<=self.__rear): 64 | msg.append((str)(self.__elements[index])) 65 | index+=1 66 | msg=" ".join(msg) 67 | msg="Queue data(Front to Rear): "+msg 68 | return msg 69 | 70 | def check_numbers(number_queue): 71 | 72 | list1=[] 73 | list2=[] 74 | 75 | while not number_queue.is_empty(): 76 | list1.append(number_queue.dequeue()) 77 | 78 | for item in list1: 79 | flag=1 80 | for j in range(1,11): 81 | if item%j!=0: 82 | flag=0 83 | break 84 | if flag==1: 85 | list2.append(item) 86 | solution_queue1=Queue(len(list2)) 87 | for item in list2: 88 | solution_queue1.enqueue(item) 89 | return solution_queue1 90 | 91 | #Add different values to the queue and test your program 92 | number_queue=Queue(5) 93 | number_queue.enqueue(13983) 94 | number_queue.enqueue(10080) 95 | number_queue.enqueue(7113) 96 | number_queue.enqueue(2520) 97 | number_queue.enqueue(2500) 98 | check_numbers(number_queue) 99 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_3/Assignment-5.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | The Lenovo Laptop Service center provides different services to its customers. As and when a job is arrived, it is allocated to an 4 | employee, if no job is assigned. Otherwise it is added to pending jobs queue. On completion of job by an employee based on elapsed time in 5 | inutes, a job in pending queue is allocated to the corresponding employee. 6 | Assumptions: 7 | 1. The service center allows to have a maximum of 10 jobs in the pending queue. 8 | 2. An employee is allocated only one job at a time 9 | 3. Each job will take a maximum of 1 hr to complete, time needed for completing a job and time elapsed are given in minutes. 10 | Solution: 11 | ''' 12 | 13 | ''' 14 | Created on Oct 20, 2019 15 | @author: HITMAN 16 | ''' 17 | #lex_auth_0127439130519060481634 18 | 19 | class Queue: 20 | def __init__(self,max_size): 21 | 22 | self.__max_size=max_size 23 | self.__elements=[None]*self.__max_size 24 | self.__rear=-1 25 | self.__front=0 26 | 27 | def is_full(self): 28 | if(self.__rear==self.__max_size-1): 29 | return True 30 | return False 31 | 32 | def is_empty(self): 33 | if(self.__front>self.__rear): 34 | return True 35 | return False 36 | 37 | def enqueue(self,data): 38 | if(self.is_full()): 39 | print("Queue is full!!!") 40 | else: 41 | self.__rear+=1 42 | self.__elements[self.__rear]=data 43 | 44 | def dequeue(self): 45 | if(self.is_empty()): 46 | print("Queue is empty!!!") 47 | else: 48 | data=self.__elements[self.__front] 49 | self.__front+=1 50 | return data 51 | 52 | def display(self): 53 | for index in range(self.__front, self.__rear+1): 54 | print(self.__elements[index]) 55 | 56 | 57 | def get_max_size(self): 58 | return self.__max_size 59 | 60 | #You can use the below __str__() to print the elements of the DS object while debugging 61 | def __str__(self): 62 | msg=[] 63 | index=self.__front 64 | while(index<=self.__rear): 65 | msg.append((str)(self.__elements[index])) 66 | index+=1 67 | msg=" ".join(msg) 68 | msg="Queue data(Front to Rear): "+msg 69 | return msg 70 | 71 | #Implement Job, Employee and Company classes here 72 | class Job: 73 | def __init__(self,name,time_needed): 74 | self.__name=name 75 | self.__time_needed=time_needed 76 | self.__time_elapsed=0 77 | 78 | def get_name(self): 79 | return self.__name 80 | 81 | def get_time_elapsed(self): 82 | return self.__time_elapsed 83 | 84 | def get_time_needed(self): 85 | return self.__time_needed 86 | 87 | 88 | def elapsed_time(self,no_of_mins): 89 | self.__time_elapsed+=no_of_mins 90 | 91 | if self.get_time_elapsed()>=self.get_time_needed(): 92 | return True 93 | else: 94 | return False 95 | 96 | def __str__(self): 97 | pass 98 | 99 | class Employee: 100 | def __init__(self,name): 101 | self.__name=name 102 | self.__allocated_job=None 103 | 104 | 105 | def set_allocated_job(self,allocated_job): 106 | self.__allocated_job=allocated_job 107 | 108 | def get_name(self): 109 | return self.__name 110 | 111 | 112 | def get_allocated_job(self): 113 | return self.__allocated_job 114 | 115 | 116 | def elapsed_time(self,no_of_mins): 117 | value=self.get_allocated_job().elapsed_time(no_of_mins) 118 | 119 | if value==True: 120 | job=self.get_allocated_job() 121 | self.set_allocated_job(None) 122 | return job 123 | else: 124 | return None 125 | 126 | 127 | 128 | 129 | class Company: 130 | def __init__(self,emp_list): 131 | self.__employees=emp_list 132 | self.__pending_jobs=Queue(10) 133 | 134 | def get_employees(self): 135 | return self.__employees 136 | 137 | 138 | def get_pending_jobs(self): 139 | return self.__pending_jobs 140 | 141 | 142 | def allocate_new_job(self,job): 143 | flag=0 144 | for item in self.get_employees(): 145 | if item.get_allocated_job()==None: 146 | item.set_allocated_job(job) 147 | flag=1 148 | break 149 | 150 | if flag==0: 151 | if self.get_pending_jobs().is_full()==False: 152 | self.get_pending_jobs().enqueue(job) 153 | 154 | def elapsed_time(self,no_of_mins): 155 | completed_jobs_list=[] 156 | 157 | flag=0 158 | for item in self.get_employees(): 159 | jobs=item.elapsed_time(no_of_mins) 160 | if jobs!=None: 161 | completed_jobs_list.append(jobs) 162 | flag=1 163 | item.set_allocated_job(self.get_pending_jobs().dequeue()) 164 | if flag==1: 165 | return completed_jobs_list 166 | else: 167 | return None 168 | #Change the values and test your programH 169 | 170 | emp1=Employee("Ken") 171 | emp2=Employee("Henry") 172 | emp3=Employee("Jack") 173 | emp4=Employee("Hen") 174 | emp5=Employee("Jill") 175 | emp_list=[emp1,emp2,emp3,emp4,emp5] 176 | company=Company(emp_list) 177 | job1=Job("job1",50) 178 | job2=Job("job2",45) 179 | job3=Job("job3",35) 180 | job4=Job("job4",400) 181 | job5=Job("job5",30) 182 | job6=Job("job6",30) 183 | job7=Job("job7",50) 184 | job8=Job("job8",25) 185 | company.allocate_new_job(job1) 186 | company.allocate_new_job(job2) 187 | company.allocate_new_job(job3) 188 | company.allocate_new_job(job4) 189 | company.allocate_new_job(job5) 190 | company.allocate_new_job(job6) 191 | company.allocate_new_job(job7) 192 | company.allocate_new_job(job8) 193 | print("Initial allocation:") 194 | for emp in company.get_employees(): 195 | print(emp.get_name(),"is allocated",emp.get_allocated_job().get_name()) 196 | print() 197 | print("Pending Jobs:") 198 | company.get_pending_jobs().display() 199 | completed_jobs=company.elapsed_time(30) 200 | '''print("Completed Jobs :") 201 | for job in completed_jobs: 202 | print(job.name)''' 203 | 204 | print("After completion:") 205 | for emp in company.get_employees(): 206 | print(emp.get_name(),"needs", emp.get_allocated_job().get_time_needed()-emp.get_allocated_job().get_time_elapsed(),"more minutes for",emp.get_allocated_job().get_name()) 207 | print() 208 | print("Pending Jobs:") 209 | company.get_pending_jobs().display() 210 | completed_jobs=company.elapsed_time(10) 211 | print("After completion:") 212 | for emp in company.get_employees(): 213 | print(emp.get_name(),"needs", emp.get_allocated_job().get_time_needed()-emp.get_allocated_job().get_time_elapsed(),"more minutes for",emp.get_allocated_job().get_name()) 214 | print() 215 | print("Pending Jobs:") 216 | company.get_pending_jobs().display() 217 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_4/Assignment-1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Given a stack of boxes in different colors. Write a python function that accepts the stack of boxes and removes those boxes having color 4 | other than the primary colors (Red, Green and Blue) from the stack. The removed boxes should be en-queued into a new queue and returned. 5 | The original stack should have only the boxes having primary colors and the order must be maintained. 6 | 7 | Perform case sensitive string comparison wherever necessary. 8 | Note: Consider the queue to be of the same size as that of the original stack. 9 | 10 | Solution: 11 | 12 | ''' 13 | #lex_auth_0127667363049881603477 14 | 15 | """*********************Queue*****************************""" 16 | class Queue: 17 | def __init__(self,max_size): 18 | 19 | self.__max_size=max_size 20 | self.__elements=[None]*self.__max_size 21 | self.__rear=-1 22 | self.__front=0 23 | 24 | def is_full(self): 25 | if(self.__rear==self.__max_size-1): 26 | return True 27 | return False 28 | 29 | def is_empty(self): 30 | if(self.__front>self.__rear): 31 | return True 32 | return False 33 | 34 | def enqueue(self,data): 35 | if(self.is_full()): 36 | print("Queue is full!!!") 37 | else: 38 | self.__rear+=1 39 | self.__elements[self.__rear]=data 40 | 41 | def dequeue(self): 42 | if(self.is_empty()): 43 | print("Queue is empty!!!") 44 | else: 45 | data=self.__elements[self.__front] 46 | self.__front+=1 47 | return data 48 | 49 | def display(self): 50 | for index in range(self.__front, self.__rear+1): 51 | print(self.__elements[index]) 52 | 53 | 54 | def get_max_size(self): 55 | return self.__max_size 56 | 57 | #You can use the below __str__() to print the elements of the DS object while debugging 58 | def __str__(self): 59 | msg=[] 60 | index=self.__front 61 | while(index<=self.__rear): 62 | msg.append((str)(self.__elements[index])) 63 | index+=1 64 | msg=" ".join(msg) 65 | msg="Queue data(Front to Rear): "+msg 66 | return msg 67 | 68 | """*********************Stack*****************************""" 69 | class Stack: 70 | def __init__(self,max_size): 71 | self.__max_size=max_size 72 | self.__elements=[None]*self.__max_size 73 | self.__top=-1 74 | 75 | def is_full(self): 76 | if(self.__top==self.__max_size-1): 77 | return True 78 | return False 79 | 80 | def is_empty(self): 81 | if(self.__top==-1): 82 | return True 83 | return False 84 | 85 | def push(self,data): 86 | if(self.is_full()): 87 | print("The stack is full!!") 88 | else: 89 | self.__top+=1 90 | self.__elements[self.__top]=data 91 | 92 | def pop(self): 93 | if(self.is_empty()): 94 | print("The stack is empty!!") 95 | else: 96 | data= self.__elements[self.__top] 97 | self.__top-=1 98 | return data 99 | 100 | def display(self): 101 | if(self.is_empty()): 102 | print("The stack is empty") 103 | else: 104 | index=self.__top 105 | while(index>=0): 106 | print(self.__elements[index]) 107 | index-=1 108 | 109 | def get_max_size(self): 110 | return self.__max_size 111 | 112 | #You can use the below __str__() to print the elements of the DS object while debugging 113 | def __str__(self): 114 | msg=[] 115 | index=self.__top 116 | while(index>=0): 117 | msg.append((str)(self.__elements[index])) 118 | index-=1 119 | msg=" ".join(msg) 120 | msg="Stack data(Top to Bottom): "+msg 121 | return msg 122 | 123 | 124 | def separate_boxes(box_stack): 125 | #Remove pass and write your logic here 126 | 127 | list=[] 128 | 129 | while(not box_stack.is_empty()): 130 | list.append(box_stack.pop()) 131 | list2=[] 132 | for item in list: 133 | if item not in ['Red','Green','Blue']: 134 | list2.append(item) 135 | for item in list2: 136 | if item in list: 137 | list.remove(item) 138 | new_queue=Queue(len(list2)) 139 | 140 | for item in list2: 141 | new_queue.enqueue(item) 142 | 143 | list.reverse() 144 | 145 | for data in list: 146 | box_stack.push(data) 147 | 148 | return new_queue 149 | 150 | #Use different values for stack and test your program 151 | box_stack=Stack(8) 152 | box_stack.push("Red") 153 | box_stack.push("Magenta") 154 | box_stack.push("Yellow") 155 | box_stack.push("Red") 156 | box_stack.push("Orange") 157 | box_stack.push("Green") 158 | box_stack.push("White") 159 | box_stack.push("Purple") 160 | print("Boxes in the stack:") 161 | box_stack.display() 162 | result=separate_boxes(box_stack) 163 | print() 164 | print("Boxes in the stack after modification:") 165 | box_stack.display() 166 | print("Boxes in the queue:") 167 | result.display() 168 | -------------------------------------------------------------------------------- /Data_Structures/Assignment_set_4/Assignment-2.py: -------------------------------------------------------------------------------- 1 | ''' 2 | 3 | Problem Statement: 4 | The International Cricket Council (ICC) wanted to do some analysis of international cricket matches held in last 10 years. 5 | 6 | Given a list containing match details as shown below: 7 | [match_detail1,match_detail2……] 8 | 9 | Format of each match_detail in the list is as shown below: 10 | country_name : championship_name : total_number_of_matches_played : number_of_matches_won 11 | 12 | Example: AUS:CHAM:5:2 means Australia has participated in Champions Trophy 5 times and have won 2 times. 13 | 14 | Write a python program which performs the following: 15 | 16 | find_matches (country_name): Accepts the country_name and returns the list of details of matches played by that country. 17 | 18 | max_wins(): Returns a dictionary containing the championship name as the key and the list of country/countries which have won the maximum 19 | number of matches in that championship as the value. 20 | 21 | find_winner(country1,country2): Accepts name of two countries and returns the country name which has won more number of matches in all 22 | championships. If both have won equal number of matches, return "Tie". 23 | 24 | Perform case sensitive string comparison wherever necessary. 25 | match_list – ['ENG:WOR:2:0', 'AUS:CHAM:5:2', 'PAK:T20:5:1', 'AUS:WOR:2:1', 'SA:T20:5:0', 'IND:T20:5:3', 'PAK:WOR:2:0', 'SA:WOR:2:0', 26 | 'SA:CHAM:5:1', 'IND:WOR:2:1'] 27 | 28 | Solution: 29 | ''' 30 | #lex_auth_0127667391112806403379 31 | 32 | def find_matches(country_name): 33 | #Remove pass and write your logic here 34 | list=[] 35 | for data in match_list: 36 | if data[0:3]==country_name: 37 | list.append(data) 38 | return list 39 | def max_wins(): 40 | #Remove pass and write your logic here 41 | dict={} 42 | list1=[] 43 | for item in match_list: 44 | list2=item.split(":") 45 | if list2[1] not in list1: 46 | list1.append(list2[1]) 47 | 48 | list=[] 49 | for data in list1: 50 | max=-2 51 | for item in match_list: 52 | l1=item.split(":") 53 | if l1[1]==data and int(l1[3])>=max: 54 | max=int(l1[3]) 55 | list.append(max) 56 | 57 | i=0 58 | for data in list1: 59 | list3=[] 60 | for item in match_list: 61 | l1=item.split(":") 62 | if l1[1]==data and int(l1[3])==list[i]: 63 | list3.append(l1[0]) 64 | i+=1 65 | dict[data]=list3 66 | return dict 67 | def find_winner(country1,country2): 68 | #Remove pass and write your logic here 69 | list2=[] 70 | max1,max2=0,0 71 | for item in match_list: 72 | list1=item.split(":") 73 | if list1[0]==country1: 74 | max1+=int(list1[3]) 75 | 76 | elif list1[0]==country2: 77 | max2+=int(list1[3]) 78 | if max1>max2: 79 | return country1 80 | elif max1)To(E): -------------------------------------------------------------------------------- 1 | Problem Statement: Write a pseudo-code which helps ( O> ) to reach ( E ). 2 | Solution: 3 | 4 | While(Start!=end) do 5 | if(Key_Found)then 6 | Unlock Lock 7 | if(Path_Ahead_Available)then 8 | move Forward 9 | else if(Diffuser_Found)then 10 | Diffuse Bombs 11 | else if(Lest_Turn_Available)then 12 | Turn Left 13 | end-if 14 | end-while 15 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/(O>)To(E)-2: -------------------------------------------------------------------------------- 1 | Problem Statement: Write a pseudo-code which helps ( O> ) to reach ( E ). 2 | 3 | solution: 4 | 5 | while(start!=end) 6 | if(Right_Turn_Available)then 7 | Turn Right 8 | end if 9 | if(red_key_found) then 10 | unlock red lock 11 | else if(blue_key_found)then 12 | unlock blue lock 13 | else 14 | Move Forward 15 | end if 16 | end-while 17 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/Armstrong Number: -------------------------------------------------------------------------------- 1 | Problem Statement: 2 | A three digit number is said to be an “Armstrong number” if the sum of the third power of its 3 | individual digits is equal to the number itself. 4 | Example: 371 is an Armstrong number as 371 = 3 3 + 7 3 + 1 3 5 | 407 is an Armstrong number as 407 = 4 3 + 0 3 + 7 3 6 | Write a pseudo-code to check whether a given three digit number is an Armstrong number. 7 | 8 | Solution: 9 | input Number 10 | Temp=Number 11 | Sum=0 12 | while(Number!=0)do 13 | Remainder=Number%10 14 | sum=sum+Number*Number*Number 15 | Number=Number/10 16 | end-while 17 | if(Temp==Sum)then 18 | display"This is an Armstrong Number" 19 | else 20 | display"This is not an Armstrong Number" 21 | end-if 22 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/CanvasProblem1: -------------------------------------------------------------------------------- 1 | Problem Statement: 2 | Write a pseudo-code to recreate the figure given in the expected canvas for the scenario. 3 | 4 | Solution: 5 | 6 | display Daylight 7 | for(counter=0,Counter<=3,Counter+1) 8 | display mountain 9 | end-for 10 | for(counter=0,counter<=2,counter+1) 11 | display sheep 12 | end-for 13 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/CanvasProblem2: -------------------------------------------------------------------------------- 1 | Problem Statement: Write a pseudo-code to recreate the figure given in the expected canvas the 2 | scenario. 3 | 4 | Solution: 5 | 6 | display Nightsky 7 | display Moon 8 | display Mountain 9 | display sheep 10 | display snow 11 | for(counter=0,counter<=2,counter+1) 12 | display tree 13 | end-for 14 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/Leap Year Problem: -------------------------------------------------------------------------------- 1 | Question: Pseudo-Code to Find if a year is leap Year or not 2 | Solution: 3 | Input Year 4 | if(year%400==0)then 5 | display"It is a leap Year" 6 | else if(year%4==0 and Year%100!=0)then 7 | display"It is a Leap Year" 8 | else: 9 | display"It is not a Leap Year" 10 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/Maze Problem: -------------------------------------------------------------------------------- 1 | Question: Write a Pseudo-Code to move from the start to the End in the Maze(Drag and Drop) 2 | 3 | Solution: 4 | 5 | while(start!=end) do 6 | if(is_Next_Block_Bomb)then 7 | move Down 8 | else 9 | Move Right 10 | end if 11 | end whlie 12 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/Minimum of 3 Numbers: -------------------------------------------------------------------------------- 1 | Qns: Write a pseudo-Code to find the minimum of 3 numbers 2 | Solution: 3 | 4 | input Number1,Number2,Number3 5 | if(Number1=90)then 23 | Scholarship=50 24 | else if(Score%2!=0)then 25 | Scholarship=5 26 | end-if 27 | else if(Branch_of_study=="Engineering")then 28 | if(Score>85)then 29 | Scholarship=50 30 | else if(score%7==0)then 31 | Scholarship=5 32 | end-if 33 | end if 34 | Scholarship_Amount=Course_Fee*scholarship/100 35 | Final_Fee=Course_Fee*Scholarship/100 36 | display Final_fee 37 | end-for 38 | 39 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 1/SumDivisibleBy4: -------------------------------------------------------------------------------- 1 | Problem Statement: Write a pseudo-code to find the sum of numbers divisible by 4. 2 | The pseudo-code must allow the user to accept a number and add it to the sum if it is divisible by 4. 3 | It should continue accepting numbers as long as the user wants to provide an input and should display 4 | the final sum. 5 | 6 | Solution: 7 | 8 | Choice='Yes' 9 | Sum=0 10 | while(Choice=='Yes') do 11 | input Number 12 | if(Number%4==0)then 13 | sum=sum+Number 14 | end-if 15 | display"do You want to Continue?(Enter Yesor No)" 16 | input Choice 17 | end-while 18 | display Sum 19 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/ProductOfThreeNumber.py: -------------------------------------------------------------------------------- 1 | Problem Statement: 2 | Write a python program tofind and display the product of three positive integer values based on the 3 | rule mentioned below: 4 | It should display the product of the three values except when one of the integer value is 7. In that 5 | case, 7 should not be included in the product and the values to its left also should not be included. 6 | If there is only one value to be considered, display that value itself. If no values can be included 7 | in the product, display -1. 8 | 9 | Solution: 10 | 11 | #lex_auth_012693711503400960120 12 | 13 | def find_product(num1,num2,num3): 14 | product=0 15 | if num1==7: 16 | product=num2*num3 17 | elif num2==7: 18 | product=num3 19 | elif num3==7: 20 | product=-1 21 | else 22 | product=num1*num2*num3 23 | return product 24 | 25 | #Provide different values for num1, num2, num3 and test your program 26 | product=find_product(7,6,2) 27 | print(product) 28 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/SideOfTriangle.py: -------------------------------------------------------------------------------- 1 | Problem Statement 2 | Write a python function to check whether three given numbers can form the sides of a triangle. 3 | Hint: Three numbers can be the sides of a triangle if none of the numbers are greater than or 4 | equal to the sum of the other two numbers. 5 | 6 | Solution: 7 | 8 | #lex_auth_012693802383794176153 9 | 10 | def form_triangle(num1,num2,num3): 11 | #Do not change the messages provided below 12 | success="Triangle can be formed" 13 | failure="Triangle can't be formed" 14 | 15 | if num1>=num2+num3: 16 | return "Failure" 17 | elif num2>=num1+num3: 18 | return "Failure" 19 | elif num3>=num1+num2: 20 | return "Failure" 21 | else: 22 | return "Success" 23 | 24 | #Use the following messages to return the result wherever necessary 25 | return success 26 | return failure 27 | 28 | #Provide different values for the variables, num1, num2, num3 and test your program 29 | num1=3 30 | num2=3 31 | num3=5 32 | form_triangle(num1, num2, num3) 33 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/calculate_bill_amount.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | FoodCorner home delivers vegetarian and non-vegetarian combos to its customer based on order. 4 | 5 | A vegetarian combo costs Rs.120 per plate and a non-vegetarian combo costs Rs.150 per plate. Their non-veg combo is really famous that 6 | they get more orders for their non-vegetarian combo than the vegetarian combo. 7 | Solution:- 8 | ''' 9 | #lex_auth_012693782475948032141 10 | 11 | def calculate_bill_amount(food_type,quantity_ordered,distance_in_kms): 12 | bill_amount=0 13 | if(food_type=="N" and quantity_ordered>0 and distance_in_kms>0): 14 | 15 | if(distance_in_kms>0 and distance_in_kms<=3): 16 | bill_amount = quantity_ordered*150 17 | elif(distance_in_kms>3 and distance_in_kms<=6): 18 | bill_amount = quantity_ordered*150 + (distance_in_kms-3)*3 19 | elif(distance_in_kms>6): 20 | bill_amount = quantity_ordered*150 + (distance_in_kms-6)*6 +3*3 21 | 22 | elif(food_type=="V" and quantity_ordered>0 and distance_in_kms>0): 23 | 24 | if(distance_in_kms>0 and distance_in_kms<=3): 25 | bill_amount = quantity_ordered*120 26 | elif(distance_in_kms>3 and distance_in_kms<=6): 27 | bill_amount = quantity_ordered*120 + (distance_in_kms-3)*3 28 | elif(distance_in_kms>6): 29 | bill_amount = quantity_ordered*120 + (distance_in_kms-6)*6 +3*3 30 | 31 | else: 32 | bill_amount=-1 33 | 34 | return bill_amount 35 | 36 | #Provide different values for food_type,quantity_ordered,distance_in_kms and test your program 37 | bill_amount=calculate_bill_amount("N",2,7) 38 | print(bill_amount) 39 | 40 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/calculate_loan.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | The Metro Bank provides various types of loans such as car loans, business loans and house loans to its account holders. Write a python 4 | program to implement the following requirements: 5 | 6 | Initialize the following variables with appropriate input values:account_number, account_balance, salary, loan_type, loan_amount_expected 7 | and customer_emi_expected. 8 | 9 | The account number should be of 4 digits and its first digit should be 1. 10 | 11 | The customer should have a minimum balance of Rupees 1 Lakh in the account. 12 | 13 | If the above rules are valid, determine the eligible loan amount and the EMI that the bank can provide to its customers based on their 14 | salary and the loan type they expect to avail. 15 | 16 | The bank would provide the loan, only if the loan amount and the number of EMI’s requested by the customer is less than or equal to the 17 | loan amount and the number of EMI’s decided by the bank respectively. 18 | 19 | Display appropriate error messages for all invalid data. If all the business rules are satisfied ,then display account number, eligible 20 | and requested loan amount and EMI’s.Test your code by providing different values for the input variables. 21 | 22 | Solution:- 23 | ''' 24 | 25 | #lex_auth_012693788748742656146 26 | 27 | def calculate_loan(account_number,salary,account_balance,loan_type,loan_amount_expected,customer_emi_expected): 28 | eligible_loan_amount=0 29 | bank_emi_expected=0 30 | eligible_loan_amount=0 31 | 32 | if(loan_type=="Car" or loan_type=="House" or loan_type=="Business"): 33 | num=str(account_number) 34 | if(len(num)!=4 or num[0]!='1'): 35 | print("Invalid account number") 36 | return 37 | elif(account_balance < 100000): 38 | print("Insufficient account balance") 39 | return 40 | 41 | if(loan_type=="Car" and customer_emi_expected<=36 and salary>25000 and loan_amount_expected <=500000): 42 | print("Account number:", account_number) 43 | print("The customer can avail the amount of Rs.", 500000) 44 | print("Eligible EMIs :", 36) 45 | print("Requested loan amount:",loan_amount_expected ) 46 | print("Requested EMI's:",customer_emi_expected) 47 | return 48 | elif(loan_type=="Car" and customer_emi_expected<=36 and salary<=25000 and loan_amount_expected <=500000): 49 | print("Invalid loan type or salary") 50 | # elif(loan_type=="Car" and customer_emi_expected>36 and salary>25000 and loan_amount_expected >500000 or loan_type=="Car" and loan_amount_expected>500000 and customer_emi_expected<=36 and salary<25000 ): 51 | # print("Invalid loan type or salary") 52 | # return 53 | 54 | 55 | elif(loan_type=="House" and customer_emi_expected<=60 and salary>50000 and loan_amount_expected <=6000000): 56 | print("Account number:", account_number) 57 | print("The customer can avail the amount of Rs.", 6000000) 58 | print("Eligible EMIs :", 60) 59 | print("Requested loan amount:",loan_amount_expected ) 60 | print("Requested EMI's:",customer_emi_expected) 61 | return 62 | elif(loan_type=="House" and customer_emi_expected<=60 and salary<=50000 and loan_amount_expected <=6000000): 63 | print("Invalid loan type or salary") 64 | 65 | # elif(loan_type=="House" and customer_emi_expected>60 and salary>50000 and loan_amount_expected >=6000000 or loan_type=="House" and customer_emi_expected<=60 and salary<50000 and loan_amount_expected >6000000): 66 | # print("Invalid loan type or salary") 67 | # return 68 | 69 | elif(loan_type=="Business" and customer_emi_expected<=84 and salary>75000 and loan_amount_expected<=7500000): 70 | print("Account number:", account_number) 71 | print("The customer can avail the amount of Rs.", 7500000) 72 | print("Eligible EMIs :", 84) 73 | print("Requested loan amount:",loan_amount_expected ) 74 | print("Requested EMI's:",customer_emi_expected) 75 | return 76 | elif(loan_type=="Business" and customer_emi_expected<=84 and salary<=75000 and loan_amount_expected<=7500000): 77 | print("Invalid loan type or salary") 78 | #elif(loan_type=="Business" and customer_emi_expected>84 and salary>75000 and loan_amount_expected>7500000 or loan_type=="Business" and customer_emi_expected<=84 and salary<75000 and loan_amount_expected>7500000): 79 | # print("The customer is not eligible for the loan") 80 | # return 81 | else: 82 | print("The customer is not eligible for the loan") 83 | 84 | else: 85 | print("Invalid loan type or salary") 86 | return 87 | 88 | 89 | #Start writing your code here 90 | #Populate the variables: eligible_loan_amount and bank_emi_expected 91 | 92 | #Use the below given print statements to display the output, in case of success 93 | #print("Account number:", account_number) 94 | #print("The customer can avail the amount of Rs.", eligible_loan_amount) 95 | #print("Eligible EMIs :", bank_emi_expected) 96 | #print("Requested loan amount:", loan_amount_expected) 97 | #print("Requested EMI's:",customer_emi_expected) 98 | 99 | #Use the below given print statements to display the output, in case of invalid data. 100 | #print("Insufficient account balance") 101 | #print("The customer is not eligible for the loan") 102 | #print("Invalid account number") 103 | #print("Invalid loan type or salary") 104 | # Also, do not modify the above print statements for verification to work 105 | 106 | 107 | #Test your code for different values and observe the results 108 | calculate_loan(1001,40000,250000,"Car",300000,30) 109 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/count_names.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python program which displays the count of the names that matches a given pattern from a list of names provided. 4 | 5 | Consider the pattern characters to be: 6 | 7 | 1. "_ at" where "_" can be one occurrence of any character 8 | 9 | 2. "%at%" where "%" can have zero or any number of occurrences of a character 10 | 11 | Solution: 12 | ''' 13 | #lex_auth_01269438070259712046 14 | 15 | def count_names(name_list): 16 | count1=0 17 | count2=0 18 | for i in name_list: 19 | if(i.endswith("at") and i!="at"): 20 | count1+=1 21 | for i in name_list: 22 | if(i.find("at")!=-1): 23 | count2+=1 24 | #start writing your code here 25 | #Populate the variables: count1 and count2 26 | 27 | # Use the below given print statements to display the output 28 | # Also, do not modify them for verification to work 29 | print("_at -> ",count1) 30 | print("%at% -> ",count2) 31 | 32 | 33 | #Provide different names in the list and test your program 34 | name_list=["Hat","Cat","rabbit","matter"] 35 | count_names(name_list) 36 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/find_max.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python program which finds the maximum number from num1 to num2 (num2 inclusive) based on the following rules. 4 | 5 | 6 | 1. Always num1 should be less than num2 7 | 8 | 2. Consider each number from num1 to num2 (num2 inclusive). Populate the number into a list, if the below conditions are satisfied 9 | 10 | a. Sum of the digits of the number is a multiple of 3 11 | 12 | b. Number has only two digits 13 | 14 | c. Number is a multiple of 5 15 | 16 | 3. Display the maximum element from the list 17 | 18 | In case of any invalid data or if the list is empty, display -1. 19 | Solution: 20 | ''' 21 | #lex_auth_012693813706604544157 22 | 23 | def find_max(num1, num2): 24 | max_num=-1 25 | if(num1!=num2): 26 | for i in range(num1,num2+1,1): 27 | if(i%5==0 and i/100<1): 28 | sum_digit=0 29 | num=i 30 | while(num!=0): 31 | rem=num%10 32 | sum_digit+=rem 33 | num//=10 34 | if(sum_digit%3==0): 35 | max_num=i 36 | 37 | return max_num 38 | 39 | #Provide different values for num1 and num2 and test your program. 40 | max_num=find_max(10,15) 41 | print(max_num) 42 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/generate_ticket.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python program to generate the ticket numbers for specified number of passengers traveling in a flight as per the details 4 | mentioned below: 5 | The ticket number should be generated as airline:src:dest:number 6 | where 7 | 1.Consider AI as the value for airline 8 | 2.src and dest should be the first three characters of the source and destination cities. 9 | 3.number should be auto-generated starting from 101. 10 | 11 | The program should return the list of ticket numbers of last five passengers. 12 | Note: If passenger count is less than 5, return the list of all generated ticket numbers. 13 | Solution:- 14 | ''' 15 | #lex_auth_012693763253788672132 16 | 17 | def generate_ticket(airline,source,destination,no_of_passengers): 18 | ticket_number_list=[] 19 | string=" " 20 | if(no_of_passengers<5): 21 | for i in range(1,no_of_passengers+1): 22 | string= airline+':'+source[0:3]+':'+destination[0:3]+':'+str(100+i) 23 | ticket_number_list.append(string) 24 | 25 | elif(no_of_passengers>=5): 26 | for i in range(-5,0): 27 | string=airline+':'+source[0:3]+':'+destination[0:3]+':'+str(no_of_passengers+100+i+1) 28 | ticket_number_list.append(string) 29 | #Use the below return statement wherever applicable 30 | return ticket_number_list 31 | 32 | #Provide different values for airline,source,destination,no_of_passengers and test your program 33 | Ticket_list=generate_ticket("AI","Bangalore","London",7) 34 | 35 | for i in Ticket_list: 36 | print(i) 37 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/get_count.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Given a list of integer values. Write a python program to check whether it contains same number in adjacent position. Display the count of 4 | such adjacent occurrences. 5 | Solution:- 6 | ''' 7 | #lex_auth_012693750719488000124 8 | 9 | def get_count(num_list): 10 | count=0 11 | 12 | for i in range(0,len(num_list)-1): 13 | if(num_list[i]==num_list[i+1]): 14 | count+=1 15 | 16 | return count 17 | 18 | #provide different values in list and test your program 19 | num_list=[1,1,5,100,-20,-20,6,0,0] 20 | print(get_count(num_list)) 21 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/make_amount.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | You have x no. of 5 rupee coins and y no. of 1 rupee coins. You want to purchase an item for amount z. The shopkeeper wants you to provide 4 | exact change. You want to pay using minimum number of coins. How many 5 rupee coins and 1 rupee coins will you use? If exact change is not 5 | possible then display -1. 6 | Solution:- 7 | ''' 8 | #lex_auth_012693780491968512136 9 | 10 | def make_amount(rupees_to_make,no_of_five,no_of_one): 11 | five_needed=0 12 | one_needed=0 13 | 14 | 15 | five=rupees_to_make//5 16 | if(five<=no_of_five): 17 | five_needed=five 18 | rupees_to_make-=five_needed*5 19 | elif(five>=no_of_five): 20 | five_needed=no_of_five 21 | rupees_to_make-=five_needed*5 22 | 23 | one=rupees_to_make//1 24 | 25 | if(one<=no_of_one): 26 | one_needed=one 27 | elif(one>=no_of_one): 28 | print(-1) 29 | return 30 | 31 | print("No. of Five needed :",five_needed) 32 | print("No. of One needed :",one_needed) 33 | 34 | 35 | 36 | 37 | #Start writing your code here 38 | #Populate the variables: five_needed and one_needed 39 | 40 | 41 | # Use the below given print statements to display the output 42 | # Also, do not modify them for verification to work 43 | 44 | #print("No. of Five needed :", five_needed) 45 | #print("No. of One needed :", one_needed) 46 | #print(-1) 47 | 48 | 49 | #Provide different values for rupees_to_make,no_of_five,no_of_one and test your program 50 | make_amount(28,8,5) 51 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 2/solve.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Write a python program to solve a classic ancient Chinese puzzle. 4 | 5 | We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have? 6 | Solution:- 7 | ''' 8 | #lex_auth_012693810762121216155 9 | 10 | def solve(heads,legs): 11 | error_msg="No solution" 12 | chicken_count=0 13 | rabbit_count=0 14 | 15 | if(heads>=legs): 16 | print(error_msg) 17 | elif(legs%2!=0): 18 | print(error_msg) 19 | else: 20 | rabbit_count=(legs-2*heads)/2 21 | chicken_count=heads-rabbit_count 22 | print(int(chicken_count),int(rabbit_count)) 23 | #Start writing your code here 24 | #Populate the variables: chicken_count and rabbit_count 25 | 26 | 27 | 28 | # Use the below given print statements to display the output 29 | # Also, do not modify them for verification to work 30 | 31 | #print(chicken_count,rabbit_count) 32 | #print(error_msg) 33 | 34 | #Provide different values for heads and legs and test your program 35 | solve(38,131) 36 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 6/10-SubstringOfaNumber.py: -------------------------------------------------------------------------------- 1 | Problem Statement: 2 | 3 | A 10-substring of a number is a substring of its digits that sum up to 10. 4 | 5 | For example, the 10-substrings of the number 3523014 are: 6 | 3523014, 3523014, 3523014, 3523014 7 | 8 | Write a python function, find_ten_substring(num_str) which accepts a string 9 | and returns the list of 10-substrings of that string. 10 | 11 | Solution: 12 | 13 | #lex_auth_01269442545042227276 14 | 15 | def find_ten_substring(num_str): 16 | list=[] 17 | for i in num_str: 18 | list.append(int(i)) 19 | list1=[] 20 | for i in range(0,len(list)): 21 | j=i 22 | sum=0 23 | string1='' 24 | string3='' 25 | while(True): 26 | if j1: 22 | list.append(key) 23 | return list 24 | 25 | list_of_numbers=[1,2,2,3,3,3,4,4,4,4] 26 | list_of_duplicates=find_duplicates(list_of_numbers) 27 | print(list_of_duplicates) 28 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 6/Palindrome_String.py: -------------------------------------------------------------------------------- 1 | Problem Statement: Write a recursive function, is_palindrome() to find out 2 | whether a string is a palindrome or not. The function should return true, if it is a 3 | palindrome. Else it should return false. 4 | 5 | Solution: 6 | 7 | #lex_auth_01269442114344550475 8 | 9 | def is_palindrome(word): 10 | string=word.lower() 11 | list1=[] 12 | list2=[] 13 | for i in string: 14 | list1+=i 15 | 16 | for i in list1[::-1]: 17 | list2+=i 18 | if list1==list2: 19 | return True 20 | else: 21 | return False 22 | 23 | result=is_palindrome("MadAMa") 24 | if(result): 25 | print("The given word is a Palindrome") 26 | else: 27 | print("The given word is not a Palindrome") 28 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment 6/SumOfPrimeFactors.py: -------------------------------------------------------------------------------- 1 | Problem Statement: Given a number n, write a program to find the sum of the largest prime factors of each of nine consecutive numbers starting from n. 2 | g(n) = f(n) + f(n+1) + f(n+2) + f(n+3) + f(n+4) + f(n+5) + f(n+6) + f(n+7) + 3 | f(n+8) 4 | where, g(n) is the sum and f(n) is the largest prime factor of n 5 | 6 | For example, 7 | g(10)=f(10)+f(11)+f(12)+f(13)+f(14)+f(15)+f(16)+f(17)+f(18) 8 | =5 + 11 + 3 + 13 + 7 + 5 + 2 + 17 + 3 9 | =66 10 | 11 | Solution: 12 | 13 | #lex_auth_01269442963365068878 14 | 15 | def find_factors(num): 16 | #Accepts a number and returns the list of all the factors of a given number 17 | factors = [] 18 | for i in range(2,(num+1)): 19 | if(num%i==0): 20 | factors.append(i) 21 | return factors 22 | 23 | def is_prime(num, i): 24 | #Accepts the number num and num/2 --> i and returns True if the number is prime ,else returns False 25 | if(i==1): 26 | return True 27 | elif(num%i==0): 28 | return False; 29 | else: 30 | return(is_prime(num,i-1)) 31 | 32 | def find_largest_prime_factor(list_of_factors): 33 | 34 | max=0 35 | for i in list_of_factors: 36 | if is_prime(i,i//2) and i>max: 37 | max=i 38 | return max 39 | #Accepts the list of factors and returns the largest prime factor 40 | 41 | def find_f(num): 42 | list=[] 43 | list=find_factors(num) 44 | return find_largest_prime_factor(list) 45 | #Accepts the number and returns the largest prime factor of the number 46 | 47 | def find_g(num): 48 | return find_f(num)+find_f(num+1)+find_f(num+2)+find_f(num+3)+find_f(num+4)+find_f(num+5)+find_f(num+6)+find_f(num+7)+find_f(num+8) 49 | #Accepts the number and returns the sum of the largest prime factors of the 9 consecutive numbers starting from the given number 50 | 51 | #Note: Invoke function(s) from other function(s), wherever applicable. 52 | 53 | print(find_g(10)) 54 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-5_(Day-5)/Even_Odd_Sum.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | Consider sample data as follows: sample_data=range(1,11) 8 | 9 | Create two functions: odd() and even() 10 | The function even() returns a list of all the even numbers from 11 | sample_data The function odd() returns a list of all the odd numbers 12 | from sample_data 13 | 14 | Create a function sum_of_numbers() which will accept the sample data 15 | and/or a function. If a function is not passed, the sum_of_numbers() 16 | should return the sum of all the numbers from sample_data If a function 17 | is passed, the sum_of_numbers() should return the sum of numbers 18 | returned from the function passed. 19 | ''' 20 | #lex_auth_0127382164803993601239 21 | 22 | #This verification is based on string match. 23 | 24 | def sum_of_numbers(list_of_num,filter_func=None): 25 | #Remove pass and write the logic here 26 | sum=0 27 | if filter_func==None: 28 | for i in list_of_num: 29 | sum+=i 30 | elif filter_func==even: 31 | list=even(list_of_num) 32 | for i in list: 33 | sum+=i 34 | elif filter_func==odd: 35 | list=odd(list_of_num) 36 | for i in list: 37 | sum+=i 38 | return sum 39 | 40 | 41 | def even(data): 42 | list=[] 43 | for i in data: 44 | if i%2==0: 45 | list.append(i) 46 | return list 47 | 48 | def odd(data): 49 | list=[] 50 | for i in data: 51 | if i%2!=0: 52 | list.append(i) 53 | return list 54 | 55 | sample_data = range(1,11) 56 | print(sum_of_numbers(sample_data,None)) 57 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-5_(Day-5)/Student_report.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement 7 | A teacher is in the process of generating few reports based on the 8 | marks scored by the students of her class in a project based assessment. 9 | Assume that the marks of her 10 students are available in a tuple. 10 | The marks are out of 25. 11 | 12 | Write a python program to implement the following functions: 13 | 14 | 1. find_more_than_average(): Find and return the percentage of students 15 | who have scored more than the average mark of the class. 16 | 17 | 2. generate_frequency(): Find how many students have scored the same 18 | marks. 19 | 3. sort_marks(): Sort the marks in the increasing order from 0 to 25. 20 | The sorted values should be populated in a list and returned. 21 | 22 | Solution: 23 | ''' 24 | #lex_auth_01269438947391897654 25 | list_of_marks=(12,18,25,24,2,5,18,20,20,21) 26 | 27 | def find_more_than_average(): 28 | #Remove pass and write your logic here 29 | sum=0 30 | for i in list_of_marks: 31 | sum+=i 32 | avg=sum/10 33 | count=0 34 | for i in list_of_marks: 35 | if i>avg: 36 | count+=1 37 | return (count*100)/10 38 | 39 | def sort_marks(): 40 | #Remove pass and write your logic here 41 | list=[] 42 | for i in list_of_marks: 43 | list.append(i) 44 | list.sort() 45 | return list 46 | 47 | def generate_frequency(): 48 | #Remove pass and write your logic here 49 | list=[0]*26 50 | for i in list_of_marks: 51 | list[i]+=1 52 | return list 53 | 54 | print(find_more_than_average()) 55 | print(generate_frequency()) 56 | print(sort_marks()) 57 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-5_(Day-5)/calculate_profit-loss.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | The road transport corporation (RTC) of a city wants to know whether a 8 | particular bus-route is running on profit or loss. 9 | 10 | Assume that the following information are given: 11 | Price per litre of fuel = 70 12 | Mileage of the bus in km/litre of fuel = 10 13 | Price(Rs) per ticket = 80 14 | 15 | The bus runs on multiple routes having different distance in kms and 16 | number of passengers. Write a function to calculate and return the 17 | profit earned (Rs) in each route. Return -1 in case of loss. 18 | 19 | Solution: 20 | ''' 21 | #lex_auth_012693816779112448160 22 | 23 | def calculate(distance,no_of_passengers): 24 | #Remove pass and write your logic here 25 | gain=no_of_passengers*80 26 | loss=distance*7 27 | profit=gain-loss 28 | 29 | if profit>=0: 30 | return profit 31 | else: 32 | return -1 33 | 34 | #Provide different values for distance, no_of_passenger and test your program 35 | distance=20 36 | no_of_passengers=50 37 | print(calculate(distance,no_of_passengers)) 38 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-5_(Day-5)/check_double.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | 8 | Write a python function, check_double(number) which accepts a whole 9 | number and returns True if it satisfies the given conditions. 10 | The number and its double should have exactly the same number of 11 | digits. 12 | 13 | Both the numbers should have the same digits ,but in different order. 14 | 15 | Otherwise it should return False. 16 | 17 | Example: If the number is 125874 and its double, 251748, contain 18 | exactly the same digits, but in a different order. 19 | 20 | Solution: 21 | ''' 22 | #lex_auth_01269441810970214471 23 | 24 | def check_double(number): 25 | #Remove pass and write your logic here 26 | n1=number 27 | n2=2*number 28 | string1=str(n1) 29 | string2=str(n2) 30 | if len(string1)!=len(string2): 31 | return False 32 | 33 | list1=[] 34 | list2=[] 35 | for i in string1: 36 | list1.append(int(i)) 37 | for i in string2: 38 | list2.append(int(i)) 39 | list1.sort() 40 | list2.sort() 41 | if list1==list2: 42 | return True 43 | else: 44 | return False 45 | 46 | #Provide different values for number and test your program 47 | print(check_double(125874)) 48 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-5_(Day-5)/find_pairs_of_numbers.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | Write a python function, find_pairs_of_numbers() which accepts a list 8 | of positive integers with no repetitions and returns count of pairs of 9 | numbers in the list that adds up to n. The function should return 0, 10 | if no such pairs are found in the list. 11 | 12 | Solution: 13 | ''' 14 | 15 | #lex_auth_01269438594930278448 16 | 17 | def find_pairs_of_numbers(num_list,n): 18 | list=num_list 19 | count=0 20 | for i in list: 21 | for j in list: 22 | if i+j==n: 23 | count+=1 24 | return count//2 25 | 26 | num_list=[1, 2, 4, 5, 6] 27 | n=6 28 | print(find_pairs_of_numbers(num_list,n)) 29 | 30 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Check_Anagram.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 4, 2019 3 | 4 | @author: HITMAN 5 | ''' 6 | ''' 7 | Problem Statement: 8 | Write a python function, check_anagram() which accepts two strings 9 | and returns True, if one string is a special anagram of another 10 | string. Otherwise returns False. The two strings are considered to 11 | be a special anagram if they contain repeating characters but none 12 | of the characters repeat at the same position. The length of the 13 | strings should be the same. 14 | ''' 15 | Solution:- 16 | 17 | #lex_auth_0127382206342184961397 18 | 19 | def check_anagram(data1,data2): 20 | string1=data1.lower() 21 | string2=data2.lower() 22 | if len(data1)!=len(data2): 23 | return False 24 | list1=[] 25 | list2=[] 26 | 27 | for i in string1: 28 | list1.append(i) 29 | for i in string2: 30 | list2.append(i) 31 | flag=1 32 | for i in range(0,len(data1)): 33 | if list1[i]==list2[i]: 34 | flag=0 35 | break 36 | if flag==0: 37 | return False 38 | list1.sort() 39 | list2.sort() 40 | 41 | if list1==list2: 42 | return True 43 | else: 44 | return False 45 | 46 | print(check_anagram("eat","tea")) 47 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Nearest_Palindrome.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 5, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | 8 | Write a python function, nearest_palindrome() which accepts a number 9 | and returns the nearest palindrome greater than the given number. 10 | 11 | Solution: 12 | ''' 13 | #lex_auth_01269443664174284882 14 | def is_Palindrome(num): 15 | string=str(num) 16 | list1=[] 17 | list2=[] 18 | for i in string: 19 | list1.append(int(i)) 20 | list2.append(int(i)) 21 | list2.reverse() 22 | if list2 == list1: 23 | return True 24 | else: 25 | return False 26 | 27 | 28 | def nearest_palindrome(number): 29 | num=number 30 | i=num+1 31 | while(True): 32 | if is_Palindrome(i)==True: 33 | return i 34 | else: 35 | i+=1 36 | 37 | 38 | 39 | number=12300 40 | print(nearest_palindrome(number)) 41 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Perfect_Number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 5, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | Write a python function to check whether the given number is a 8 | perfect number or not. The function should returns true if the 9 | number is a perfect number, else it should returns false. 10 | 11 | Hint: Perfect number is a positive whole number that is equal to 12 | the sum of its proper divisors. The first perfect number is 6 as 13 | the sum of its proper positive divisors, 1,2 and 3 is 6. Other perfect numbers are 28, 496, 8128 ... 14 | 15 | Extend the program written for the above problem to write another 16 | function to find all perfect numbers in a given list of numbers. 17 | Populate the perfect numbers in a list and return the list. 18 | If no perfect numbers found, return an empty list. 19 | 20 | Solution: 21 | 22 | ''' 23 | #lex_auth_01269446533799116898 24 | 25 | def check_perfect_number(number): 26 | string=str(number) 27 | list=[] 28 | sum=0 29 | if number==0: 30 | return False 31 | for i in range(1,(number//2)+1): 32 | if number%i==0: 33 | list.append(i) 34 | sum+=i 35 | if sum==number: 36 | return True 37 | else: 38 | return False 39 | 40 | def check_perfectno_from_list(no_list): 41 | #start writing your code here 42 | list=[] 43 | for i in no_list: 44 | if check_perfect_number(i): 45 | list.append(i) 46 | return list 47 | 48 | perfectno_list=check_perfectno_from_list([18,6,4,2,1,28]) 49 | print(perfectno_list) 50 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Remove_Duplicates.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 5, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | Write a python function, remove_duplicates() which accepts a string 8 | and removes all duplicate characters from the given string and 9 | return it. 10 | 11 | #lex_auth_01269446319507046499 12 | ''' 13 | Solution: 14 | 15 | def remove_duplicates(value): 16 | 17 | list=[] 18 | list1=[] 19 | for i in value: 20 | list.append(i) 21 | for i in list: 22 | if i not in list1: 23 | list1.append(i) 24 | 25 | string='' 26 | for i in list1: 27 | string+=i 28 | return string 29 | 30 | print(remove_duplicates("11223445566666ababzzz@@@123#*#*")) 31 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Ticket_List.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 5, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | 8 | Write a python program to help an airport manager to generate few 9 | statistics based on the ticket details available for a day.Go through 10 | the below program and complete it based on the comments mentioned in it. 11 | 12 | Note: Perform case sensitive string comparisons wherever necessary. 13 | 14 | Solution: 15 | 16 | #lex_auth_0127382193364008961449 17 | 18 | #Sample ticket list - ticket format: "flight_no:source:destination:ticket_no" 19 | #Note: flight_no has the following format - "airline_name followed by three digit number 20 | ''' 21 | #Global variable 22 | ticket_list=["AI567:MUM:LON:014","AI077:MUM:LON:056", "BA896:MUM:LON:067", "SI267:MUM:SIN:145","AI077:MUM:CAN:060","SI267:BLR:MUM:148","AI567:CHE:SIN:015","AI077:MUM:SIN:050","AI077:MUM:LON:051","SI267:MUM:SIN:146"] 23 | 24 | def find_passengers_flight(airline_name="AI"): 25 | #This function finds and returns the number of passengers travelling in the specified airline. 26 | count=0 27 | for i in ticket_list: 28 | string_list=i.split(":") 29 | if(string_list[0].startswith(airline_name)): 30 | count+=1 31 | return count 32 | 33 | def find_passengers_destination(destination): 34 | #Write the logic to find and return the number of passengers traveling to the specified destination 35 | list=[] 36 | count=0 37 | for i in ticket_list: 38 | list=i.split(":") 39 | if list[2]==destination: 40 | count+=1 41 | return count 42 | 43 | def find_passengers_per_flight(): 44 | '''Write the logic to find and return a list having number of passengers traveling per flight based on the details in the ticket_list 45 | In the list, details should be provided in the format: 46 | [flight_no:no_of_passengers, flight_no:no_of_passengers, etc.].''' 47 | 48 | list1=[] 49 | list2=[] 50 | list3=[] 51 | list4=[] 52 | for i in ticket_list: 53 | list1=i.split(":") 54 | list2.append(list1[0]) 55 | 56 | for i in list2: 57 | num=0 58 | string='' 59 | if i[:2] not in list3: 60 | num=list2.count(i) 61 | list3.append(i[:2]) 62 | string+=i 63 | string+=":" 64 | string+=str(num) 65 | list4.append(string) 66 | return list4 67 | 68 | 69 | def sort_passenger_list(): 70 | #Write the logic to sort the list returned from find_passengers_per_flight() function in the descending order of number of passengers 71 | list=find_passengers_per_flight() 72 | list3=[] 73 | list4=[] 74 | dict={} 75 | for i in list: 76 | list3=i.split(":") 77 | list4.append(int(list3[1])) 78 | dict[list3[0]]=int(list3[1]) 79 | list4.sort() 80 | list4.reverse() 81 | list=[] 82 | for i in list4: 83 | for key,value in dict.items(): 84 | string='' 85 | if i == dict[key]: 86 | string+=key 87 | string+=":" 88 | string+=str(dict[key]) 89 | list.append(string) 90 | return list 91 | 92 | #Provide different values for airline_name and destination and test your program. 93 | print(find_passengers_flight("AI")) 94 | print(find_passengers_destination("LON")) 95 | print(sort_passenger_list()) 96 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/ValidateCreditCardNumber.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | 8 | Use Luhn algorithm to validate a credit card number. 9 | A credit card number has 16 digits, the last digit being the check 10 | digit. A credit card number can be validated using Luhn algorithm as 11 | follows: 12 | 13 | Step 1a: From the second last digit (inclusive), double the value of 14 | every second digit. 15 | Suppose the credit card number is 1456734512345698. 16 | Take the double of 9,5,3,1,4,7,5 and 1 17 | i.e., 18, 10, 6, 2, 8, 14, 10 and 2 18 | 19 | Note: If any number is greater than 9, then sum the digits of that 20 | number. 21 | i.e., 9, 1, 6, 2, 8, 5 ,1 and 2 22 | 23 | Step 1b: Sum the numbers obtained in Step 1a. 24 | i.e., 34 25 | 26 | Step 2: Sum the remaining digits in the credit card and add it 27 | with the sum from Step 1b. 28 | i.e., 34 + (8+6+4+2+5+3+6+4) = 72 29 | 30 | Step 3: If the total is divisible by 10 then the number is valid else 31 | it is not valid. 32 | Write a function, validate_credit_card_number(), which accepts a 33 | 16 digit credit card number and returns true if it is valid as per 34 | Luhn’s algorithm or false, if it is invalid. 35 | 36 | Solution: 37 | ''' 38 | #lex_auth_01269445968039936095 39 | def sum_digit(number): 40 | num=number 41 | sum=0 42 | while(num!=0): 43 | rem=num%10 44 | sum+=rem 45 | num//=10 46 | return sum 47 | def validate_credit_card_number(card_number): 48 | #start writing your code here 49 | string=str(card_number) 50 | list=[] 51 | for i in string: 52 | list.append(int(i)) 53 | sum=0 54 | for i in range(len(string)-2,-1,-2): 55 | list[i]*=2 56 | if list[i]>9: 57 | list[i]=sum_digit(list[i]) 58 | for i in range(0,len(string)): 59 | sum+=list[i] 60 | if sum%10==0: 61 | return True 62 | else: 63 | return False 64 | 65 | card_number= 1456734512345698 #4539869650133101 #1456734512345698 # #5239512608615007 66 | result=validate_credit_card_number(card_number) 67 | if(result): 68 | print("credit card number is valid") 69 | else: 70 | print("credit card number is invalid") 71 | 72 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/Validate_all.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 6, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement: 7 | Write a python program to validate the details provided by a user as 8 | part of registering to a web application. 9 | 10 | Write a function validate_name(name) to validate the user name 11 | Name should not be empty, name should not exceed 15 characters 12 | Name should contain only alphabets 13 | 14 | Write a function validate_phone_no(phoneno) to validate the phone number 15 | Phone number should have 10 digits 16 | Phone number should not have any characters or special characters 17 | All the digits of the phone number should not be same. 18 | Example: 9999999999 is not a valid phone number 19 | 20 | Write a function validate_email_id(email_id) to validate email Id 21 | It should contain one '@' character and '.com' 22 | '.com' should be present at the end of the email id. 23 | Domain name should be either 'gmail', 'yahoo' or 'hotmail' 24 | Note: Consider the format of email id to be username@domain_name.com 25 | 26 | All the functions should return true if the corresponding value is 27 | valid. Otherwise, it should return false. 28 | 29 | Write a function validate_all(name,phone_no,email_id) which should 30 | invoke appropriate functions to validate the arguments passed to it 31 | and display appropriate message. Refer the comments provided in the 32 | code. 33 | 34 | Solution: 35 | 36 | ''' 37 | #lex_auth_012694465248329728100 38 | 39 | def validate_name(name): 40 | #Start writing your code here 41 | if name==" " or len(name)>15: 42 | return False 43 | for i in name: 44 | if i.isdigit(): 45 | return False 46 | return True 47 | 48 | 49 | def validate_phone_no(phno): 50 | #Start writing your code here 51 | string=str(phno) 52 | if len(string)!=10: 53 | return False 54 | for i in string: 55 | if i.isalpha(): 56 | return False 57 | list=[] 58 | for i in string: 59 | list.append(int(i)) 60 | for i in range(1,len(string)): 61 | if list[i]!=list[i-1]: 62 | return True 63 | return False 64 | 65 | def validate_email_id(email_id): 66 | #Start writing your code here 67 | if email_id.count("@")!=1 or email_id.count(".com")!=1: 68 | return False 69 | if email_id.endswith(".com")==False: 70 | return False 71 | if "gmail" in email_id or "yahoo" in email_id or "hotmail" in email_id: 72 | pass 73 | else: 74 | return False 75 | return True 76 | 77 | def validate_all(name,phone_no,email_id): 78 | #Start writing your code here 79 | # Use the below given print statements to display appropriate messages 80 | # Also, do not modify them for verification to work 81 | #print("Invalid Name") 82 | #print("Invalid phone number") 83 | #print("Invalid email id") 84 | #print("All the details are valid") 85 | if validate_email_id(email_id)==False: 86 | print("Invalid email id") 87 | elif validate_name(name)==False: 88 | print("Invalid Name") 89 | elif validate_phone_no(phone_no)==False: 90 | print("Invalid phone number") 91 | else: 92 | print("All the details are valid") 93 | #Provide different values for name, phone_no and email_id and test your program 94 | validate_all("Tina", "9994599998", "tina@yahoo.com") 95 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment-7(Day-7)/get_circular_prime_count.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Created on Oct 5, 2019 3 | 4 | @author: HITMAN 5 | 6 | Problem Statement 7 | The number, 197, is called a circular prime because all rotations of 8 | the digits: 197, 971, and 719, are themselves prime. 9 | There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 10 | 37, 71, 73, 79, and 97. 11 | Write a python program to find and display the number of circular 12 | primes less than the given limit. 13 | 14 | Solution: 15 | ''' 16 | #lex_auth_01269446157664256093 17 | 18 | def check_prime(number): 19 | #remove pass and write your logic here. if the number is prime return true, else return false 20 | for i in range(2,(number//2)+1): 21 | if number%i==0: 22 | return False 23 | return True 24 | 25 | def rotate(l,n): 26 | return l[n:] + l[:n] 27 | def rotations(num): 28 | #remove pass and write your logic here. It should return the list of different combinations of digits of the given number. 29 | #Rotation should be done in clockwise direction. For example, if the given number is 197, the list returned should be [197, 971, 719] 30 | string=str(num) 31 | list=[] 32 | for i in string: 33 | list.append(int(i)) 34 | list1=[] 35 | for i in range(0,len(string)): 36 | string='' 37 | list2=rotate(list,i) 38 | for j in list2: 39 | string+=str(j) 40 | if int(string) not in list1: 41 | list1.append(int(string)) 42 | return list1 43 | def get_circular_prime_count(limit): 44 | #remove pass and write your logic here.It should return the count of circular prime numbers below the given limit. 45 | count=0 46 | for i in range (2,limit): 47 | list=rotations(i) 48 | flag=1 49 | for j in list: 50 | num=check_prime(int(j)) 51 | if num==False: 52 | flag=0 53 | break 54 | if flag==1: 55 | count+=1 56 | return count 57 | 58 | #Provide different values for limit and test your program 59 | print(get_circular_prime_count(1000)) 60 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/calculate_bill_amount.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | ARS Gems Store sells different varieties of gems to its customers. 4 | 5 | Write a Python program to calculate the bill amount to be paid by a customer based on the list of gems and quantity purchased. 6 | Any purchase with a total bill amount above Rs.30000 is entitled for 5% discount. If any gem required by the customer is not available in 7 | the store, then consider total bill amount to be -1. 8 | 9 | Assume that quantity required by the customer for any gem will always be greater than 0. 10 | 11 | Perform case-sensitive comparison wherever applicable. 12 | Solution: 13 | ''' 14 | #lex_auth_012693795044450304151 15 | 16 | def calculate_bill_amount(gems_list, price_list, reqd_gems,reqd_quantity): 17 | bill_amount=0 18 | 19 | i=0 20 | for item in reqd_gems: 21 | if item in gems_list: 22 | #for i in range(0,len(reqd_quantity)): 23 | pos=gems_list.index(item) 24 | bill_amount+=reqd_quantity[i]*price_list[pos] 25 | i+=1 26 | 27 | else: 28 | bill_amount=-1 29 | break; 30 | if bill_amount>30000: 31 | bill_amount-=bill_amount*0.05 32 | return bill_amount 33 | 34 | #List of gems available in the store 35 | gems_list=["Emerald","Ivory","Jasper","Ruby","Garnet"] 36 | 37 | #Price of gems available in the store. gems_list and price_list have one-to-one correspondence 38 | price_list=[1760,2119,1599,3920,3999] 39 | 40 | #List of gems required by the customer 41 | reqd_gems=["Ivory","Emerald","Garnet"] 42 | 43 | #Quantity of gems required by the customer. reqd_gems and reqd_quantity have one-to-one correspondence 44 | reqd_quantity=[3,10,12] 45 | 46 | bill_amount=calculate_bill_amount(gems_list, price_list, reqd_gems, reqd_quantity) 47 | print(bill_amount) 48 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/calculate_total_chocolates.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | A teacher is conducting a camp for a group of five children. Based on their performance and behavior during the camp, the teacher rewards 4 | them with chocolates. 5 | 6 | Write a Python function to 7 | 8 | 1. Find the total number of chocolates received by all the children put together. 9 | Assume that each child is identified by an id and it is stored in a tuple and the number of chocolates given to each child is stored in a 10 | list. 11 | 2. The teacher also rewards a child with few extra chocolates for his/her best conduct during the camp. 12 | 13 | If the number of extra chocolates is less than 1, an error message "Extra chocolates is less than 1", should be displayed. 14 | 15 | If the given child Id is invalid, an error message "Child id is invalid" should be displayed. Otherwise, the extra chocolates provided for 16 | the child must be added to his/her existing number of chocolates and display the list containing the total number of chocolates received 17 | by each child. 18 | Solution:- 19 | ''' 20 | 21 | #lex_auth_01269442027919769669 22 | 23 | #Global variables 24 | child_id=(10,20,30,40,50) 25 | chocolates_received=[12,5,3,4,6] 26 | 27 | def calculate_total_chocolates(): 28 | sum=0 29 | for i in chocolates_received: 30 | sum+=i 31 | return sum 32 | 33 | def reward_child(child_id_rewarded,extra_chocolates): 34 | 35 | if extra_chocolates < 1: 36 | print("Extra chocolates is less than 1") 37 | 38 | elif child_id_rewarded not in child_id: 39 | print("Child id is invalid") 40 | else: 41 | pos=child_id.index(child_id_rewarded) 42 | chocolates_received[pos]+=extra_chocolates 43 | print(chocolates_received) 44 | 45 | 46 | 47 | # Use the below given print statements to display the output 48 | # Also, do not modify them for verification to work 49 | 50 | #print("Extra chocolates is less than 1") 51 | #print("Child id is invalid") 52 | #print(chocolates_received) 53 | 54 | 55 | print(calculate_total_chocolates()) 56 | #Test your code by passing different values for child_id_rewarded,extra_chocolates 57 | reward_child(20,2) 58 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/check_palindrome.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a function, check_palindrome() to check whether the given string is a palindrome or not. The function should return true if it is a 4 | palindrome else it should return false. 5 | 6 | Note: Initialize the string with various values and test your program. Assume that all the letters in the given string are all of the same 7 | case. Example: MAN, civic, WOW etc. 8 | Solutions: 9 | ''' 10 | #lex_auth_012693819159732224162 11 | 12 | def check_palindrome(word): 13 | string=word[::-1] 14 | if string==word: 15 | return True 16 | else: 17 | return False 18 | 19 | status=check_palindrome("malayalam") 20 | if(status): 21 | print("word is palindrome") 22 | else: 23 | print("word is not palindrome") 24 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/create_largest_number.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python function, create_largest_number(), which accepts a list of numbers and returns the largest number possible by concatenating 4 | the list of numbers. 5 | Note: Assume that all the numbers are two digit numbers. 6 | Solutions: 7 | ''' 8 | #lex_auth_01269441913243238467 9 | 10 | def create_largest_number(number_list): 11 | max=0 12 | number_list.sort() 13 | number_list.reverse() 14 | 15 | for i in number_list: 16 | max=max*100+i 17 | return max 18 | 19 | 20 | number_list=[23,45,67] 21 | largest_number=create_largest_number(number_list) 22 | print(largest_number) 23 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/encode.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Given a string containing uppercase characters (A-Z), compress the string using Run Length encoding. Repetition of character has to be 4 | replaced by storing the length of that run. 5 | 6 | Write a python function which performs the run length encoding for a given String and returns the run length encoded String. 7 | Solutions:- 8 | ''' 9 | #lex_auth_012693816331657216161 10 | 11 | def encode(message): 12 | 13 | start=0 14 | end=len(message) 15 | string="" 16 | i=0 17 | p=0 18 | while(i=given_year and p<15): 14 | if(i%400==0 or (i%100!=0 and i%4==0)): 15 | list_of_leap_years.append(i) 16 | p+=1 17 | i+=1 18 | return list_of_leap_years 19 | 20 | list_of_leap_years=find_leap_years(2000) 21 | print(list_of_leap_years) 22 | 23 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-3/translate.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Represent a small bilingual (English-Swedish) glossary given below as a Python dictionary 4 | 5 | {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"} 6 | 7 | and use it to translate your Christmas wishes from English into Swedish. 8 | 9 | That is, write a python function translate() that accepts the bilingual dictionary and a list of English words (your Christmas wish) and 10 | returns a list of equivalent Swedish words. 11 | Solutions:- 12 | ''' 13 | #lex_auth_012693774187716608134 14 | 15 | def translate(bilingual_dict,english_words_list): 16 | 17 | swedish_words_list=[] 18 | 19 | for key1 in english_words_list: 20 | for key2 in bilingual_dict: 21 | if key1==key2: 22 | swedish_words_list.append(bilingual_dict[key2]) 23 | 24 | return swedish_words_list 25 | 26 | 27 | bilingual_dict= {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"} 28 | english_words_list=["merry","christmas"] 29 | print("The bilingual dict is:",bilingual_dict) 30 | print("The english words are:",english_words_list) 31 | 32 | swedish_words_list=translate(bilingual_dict, english_words_list) 33 | print("The equivalent swedish words are:",swedish_words_list) 34 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-4/encrypt_sentence.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python function, encrypt_sentence() which accepts a message and encrypts it based on rules given below and returns the encrypted 4 | message. 5 | Words at odd position -> Reverse It 6 | Words at even position -> Rearrange the characters so that all consonants appear before the vowels and their order should not change 7 | 8 | Solution: 9 | ''' 10 | #lex_auth_01269444195664691284 11 | 12 | def encrypt_sentence(sentence): 13 | list1=sentence.split(' ') 14 | string='' 15 | sentence='' 16 | for i in range(0,len(list1)): 17 | if i%2==0: 18 | string1=list1[i][::-1] 19 | string+=string1 20 | #string+=" " 21 | elif i%2!=0: 22 | string2=list1[i] 23 | string3='' 24 | string4='' 25 | for j in range(0,len(string2)): 26 | if string2[j]=='a' or string2[j]=='e' or string2[j]=='i' or string2[j]=='o' or string2[j]=='u': 27 | string3+=string2[j] 28 | else: 29 | string4+=string2[j] 30 | string+=string4+string3 31 | string+=" " 32 | sentence=string[:len(string)-1] 33 | return sentence 34 | sentence="The sun rises in the east" 35 | encrypted_sentence=encrypt_sentence(sentence) 36 | print(encrypted_sentence) 37 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-4/find_common_characters.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement 3 | Write a python program to display all the common characters between two strings. Return -1 if there are no matching characters. 4 | 5 | Note: Ignore blank spaces if there are any. Perform case sensitive string comparison wherever necessary. 6 | Solution: 7 | ''' 8 | #lex_auth_012693825794351104168 9 | 10 | def find_common_characters(msg1,msg2): 11 | common='' 12 | flag=0 13 | for i in range(0,len(msg1)): 14 | for j in range(0,len(msg2)): 15 | if msg1[i]!=" ": 16 | if msg1[i]==msg2[j]: 17 | if msg1[i] not in common: 18 | common=common+msg1[i] 19 | flag=1 20 | #my_common=set(common) 21 | if flag==1: 22 | return common 23 | elif flag==0: 24 | return -1 25 | msg1="I like Python" 26 | msg2="Java is a very popular language" 27 | common_characters=find_common_characters(msg1,msg2) 28 | print(common_characters) 29 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-4/find_correct.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python function, find_correct() which accepts a dictionary and returns a list as per the rules mentioned below. 4 | The input dictionary will contain correct spelling of a word as key and the spelling provided by a contestant as the value. 5 | 6 | The function should identify the degree of correctness as mentioned below: 7 | CORRECT, if it is an exact match 8 | ALMOST CORRECT, if no more than 2 letters are wrong 9 | WRONG, if more than 2 letters are wrong or if length (correct spelling versus spelling given by contestant) mismatches. 10 | 11 | and return a list containing the number of CORRECT answers, number of ALMOST CORRECT answers and number of WRONG answers. 12 | Assume that the words contain only uppercase letters and the maximum word length is 10. 13 | Solution:- 14 | ''' 15 | #lex_auth_01269444890062848087 16 | 17 | def find_correct(word_dict): 18 | list=[0,0,0] 19 | for key,value in word_dict.items(): 20 | match=0 21 | no_match=0 22 | 23 | if len(key)!=len(value): 24 | list[2]+=1 25 | continue 26 | for i in range(0,len(key)): 27 | if key[i]==value[i]: 28 | match+=1 29 | elif key[i]!=value[i]: 30 | no_match+=1 31 | if match==len(key): 32 | list[0]+=1 33 | continue 34 | elif no_match>0 and no_match<=2: 35 | list[1]+=1 36 | continue 37 | elif no_match>2: 38 | list[2]+=1 39 | continue 40 | 41 | 42 | return list 43 | 44 | 45 | 46 | word_dict={"THEIR": "THEIR","BUSINESS":"BISINESS","WINDOWS":"WINDMILL","WERE":"WEAR","SAMPLE":"SAMPLE"} 47 | print(find_correct(word_dict)) 48 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-4/max_frequency_word_counter.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write a python program that accepts a text and displays a string which contains the word with the largest frequency in the text and the 4 | frequency itself separated by a space. 5 | 6 | Rules: 7 | 8 | The word should have the largest frequency. 9 | 10 | In case multiple words have the same frequency, then choose the word that has the maximum length. 11 | 12 | Assumptions: 13 | 14 | The text has no special characters other than space. 15 | 16 | The text would begin with a word and there will be only a single space between the words. 17 | 18 | Perform case insensitive string comparisons wherever necessary. 19 | Solution: 20 | ''' 21 | #lex_auth_0127382283825971201450 22 | 23 | def max_frequency_word_counter(data): 24 | word='' 25 | frequency=0 26 | 27 | dict={} 28 | data1=data.lower() 29 | list=data1.split() 30 | my_set=set(list) 31 | list2=[] 32 | for i in my_set: 33 | dict[i]=list.count(i) 34 | 35 | max=0 36 | 37 | for key,value in dict.items(): 38 | if maxo and p>e: 28 | max=p 29 | elif o>p and o>e: 30 | max=o 31 | else: 32 | max=e 33 | if max==p: 34 | speciality="Pediatrics" 35 | elif max==o: 36 | speciality="Orthopedics" 37 | else: 38 | speciality="ENT" 39 | return speciality 40 | 41 | #provide different values in the list and test your program 42 | patient_medical_speciality_list=[301,'P',302, 'P' ,305, 'P' ,401, 'E' ,656, 'E'] 43 | medical_speciality={"P":"Pediatrics","O":"Orthopedics","E":"ENT"} 44 | speciality=max_visited_speciality(patient_medical_speciality_list,medical_speciality) 45 | print(speciality) 46 | -------------------------------------------------------------------------------- /Fundamental Programming Part 1/Assignment_Set-4/sms_encoding.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Problem Statement: 3 | Write python function, sms_encoding() which accepts a sentence and converts it into an abbreviated sentence to be sent as SMS and returns 4 | the abbreviated sentence. 5 | 6 | Rules are as follows: 7 | a. Spaces are to be retained as is 8 | b. Each word should be encoded separately 9 | 10 | If a word has only vowels then retain the word as is 11 | 12 | If a word has a consonant (at least 1) then retain only those consonants 13 | 14 | Note: Assume that the sentence would begin with a word and there will be only a single space between the words. 15 | Solution: 16 | ''' 17 | #lex_auth_01269444961482342489 18 | 19 | def sms_encoding(data): 20 | list=data.split(" ") 21 | string='' 22 | sentence='' 23 | for data in list: 24 | cons=0 25 | con_string='' 26 | for i in range(0,len(data)): 27 | if data[i] not in ['a','e','i','o','u','A','E','I','O','U']: 28 | cons+=1 29 | con_string+=data[i] 30 | if cons>=1: 31 | string+=con_string 32 | elif cons==0: 33 | string+=data 34 | string+=" " 35 | sentence=string[:len(string)-1] 36 | return sentence 37 | data="I love Python" 38 | print(sms_encoding(data)) 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python-in-Infosys 2 | Contains Solutions of all Assignments from Infosys Generic Training on Python Fundamentals Part-1. 3 | NOTE: Handle with Care. 4 | --------------------------------------------------------------------------------