Subscribe Us

Python Programming

 Problem 1 : Addition without arithmetic operator 

def addition(a,b):

while(b!=0):

carry=a&b

a=a^b

b=carry>>1

return a

def main():

a=int(input("enter first number"))

b=int(input("enter second number"))

print("sum is", int(addition(a,b)))


main()



Problem 2 : Given a string, find the length of the longest substring in it with no more than K distinct characters.


Code (python)


def longest_substring_with_k_distinct(str, k):

  window_start = 0

  max_length = 0

  char_frequency = {}

 

  # in the following loop we'll try to extend the range [window_start, window_end]

  for window_end in range(len(str)):

    right_char = str[window_end]

    if right_char not in char_frequency:

      char_frequency[right_char] = 0

    char_frequency[right_char] += 1

 

    # shrink the sliding window, until we are left with 'k' distinct characters in the char_frequency

    while len(char_frequency) > k:

      left_char = str[window_start]

      char_frequency[left_char] -= 1

      if char_frequency[left_char] == 0:

        del char_frequency[left_char]

      window_start += 1  # shrink the window

    # remember the maximum length so far

    max_length = max(max_length, window_end-window_start + 1)

  return max_length

 

 

def main():

  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("araaci", 2)))

  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("araaci", 1)))

  print("Length of the longest substring: " + str(longest_substring_with_k_distinct("cbbebi", 3)))

 

 

main()



Problem 3

Given an array of characters where each character represents a fruit tree, you are given two baskets and your goal is to put maximum number of fruits in each basket. The only restriction is that each basket can have only one type of fruit. (Concept)


Code #

Here is what our algorithm will look like,

 

def fruits_into_baskets(fruits):

  window_start = 0

  max_length = 0

  fruit_frequency = {}

 

  # try to extend the range [window_start, window_end]

  for window_end in range(len(fruits)):

    right_fruit = fruits[window_end]

    if right_fruit not in fruit_frequency:

      fruit_frequency[right_fruit] = 0

    fruit_frequency[right_fruit] += 1

 

    # shrink the sliding window, until we are left with '2' fruits in the fruit frequency dictionary

    while len(fruit_frequency) > 2:

      left_fruit = fruits[window_start]

      fruit_frequency[left_fruit] -= 1

      if fruit_frequency[left_fruit] == 0:

        del fruit_frequency[left_fruit]

      window_start += 1  # shrink the window

    max_length = max(max_length, window_end-window_start + 1)

  return max_length

 

 

def main():

  print("Maximum number of fruits: " + str(fruits_into_baskets(['A', 'B', 'C', 'A', 'C'])))

  print("Maximum number of fruits: " + str(fruits_into_baskets(['A', 'B', 'C', 'B', 'B', 'C'])))

 

 

main()


Problem 4


Cyclic Sort (easy)

Problem Statement

We are given an array containing ‘n’ objects. Each object, when created, was assigned a unique number from 1 to ‘n’ based on their creation sequence. This means that the object with sequence number ‘3’ was created just before the object with sequence number ‘4’. (Concept)


Python Code 

def cyclic_sort(nums):

  i = 0

  while i < len(nums):

    j = nums[i] - 1

    if nums[i] != nums[j]:

      nums[i], nums[j] = nums[j], nums[i]  # swap

    else:

      i += 1

  return nums

 

 

def main():

  print(cyclic_sort([3, 1, 5, 4, 2]))

  print(cyclic_sort([2, 6, 4, 3, 1, 5]))

  print(cyclic_sort([1, 5, 6, 4, 3, 2]))

 

 

main()

 

 


0 Comments:

Post a Comment

If you have any doubts . Please let me know.

Powered by Blogger.

Ad Code

Responsive Advertisement

Ad Code

Responsive Advertisement

Featured post

Search This Blog

Recently added book names

THE HTML AND CSS WORKSHOP   | MICROSOFT POWER BI COOKBOOK   | MongoDB in Action, 2nd Edition  | ADVANCED DEEP LEARNING WITH PYTHON   | Cracking Codes with Python An Introduction to Building and Breaking  | Moris Mano Degital Design 3rd Edition  | Beginning App Development with Flutter by Rap Payne  |react hooks in Action - John Larsen   | Artificial Intelligence A Modern Approach Third Edition Stuart Russel  | Data Structures and Algorithms - Narasimha Karumanchi   | Thomas S.M. - PostgreSQL High Availability Cookbook - 2017  | Gunnard Engebreth PHP 8 Revealed Use Attributes the JIT Compiler   | ICSE Class X Computer Application Notes   | INTERNET OF THINGS PROJECTS WITH ESP32   | 100 aptitude trick(102pgs)s   | OBJECT_ORIENTED_PROGRAMMING Question & Answer   | C questions and answer   | Full_Book_Python_Data_Structures_And_Algorithm   | Jira 8 Administration Cookbook Third Edition  | KALI LINUX WIRELESS PENETRATION TESTING BEGINNERS GUIDE THIRD EDITION - Cameron Buchanan, Vivek Ramachandran  HTML5 & javascript By :- Jeanine Meyer   | Python For Beginners Ride The Wave Of Artificial Intelligence   | HackingTheXbox   | Introduction to Algorithms 3rd.Edition - (CLRS)   | The C++ Programming Language - Bjarne Stroustrup   | Modern C++ Programming Cookbook - Marius Bancila   | Java The Complete Reference Eleventh Edition   Data_Communications and Networking 4th Ed Behrouz A Forouzan   | DevOps with Kubernetes - Hideto Saito   | The-Linux-Command-Line-A-Complete-Introduction   | Assembly Language for X86 Processors KIP R. Irvine   | Effective_Modern_C++ - Scott Meyer

Contact Form

Name

Email *

Message *

Followers

Mobile Logo Settings

Mobile Logo Settings
image

Computer Training School Regd. under Govt. of West Bengal Society Act 1961

Header Ads Widget

Responsive Advertisement

Hot Widget

random/hot-posts

Recent in Sports

Popular Posts

Most Popular

Popular Posts

Labels

Blogger templates