sidrs

FDS (KR) - Ass 1 (String Operations)

Jul 31st, 2024 (edited)
484
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. ####################################
  2. ##  KR LAB 1 - STRING OPERATIONS  ##
  3. ####################################
  4.  
  5. ##########################
  6. ##  1. CHARACTER COUNT  ##
  7. ##########################
  8. def charcount():
  9.     word = input("Enter a word: ")
  10.     count = 0
  11.     for char in word:
  12.         count += 1
  13.     print(count)
  14. # charcount()
  15.  
  16.  
  17.  
  18. ###############################
  19. ##  2. WORD WITH MAX LENGTH  ##
  20. ###############################
  21. def maxLengthWord():
  22.     statement = input("Enter a sentence: ")
  23.     statement = statement.split(" ")
  24.     max = 0
  25.     for i in statement:
  26.         if len(i) >= max:
  27.             max = len(i)
  28.             word = i
  29.     print("The word with max length is: " + word)
  30.     print("and its length is: " + str(max))
  31. # maxLengthWord()
  32.  
  33.  
  34.  
  35. ######################################
  36. ##  3. FREQUENCY OF A GIVEN LETTER  ##
  37. ######################################
  38. def getLetterFrequency():
  39.     word = input("Enter a word: ")
  40.     letter = input("Enter the letter to be checked: ")
  41.     count = 0
  42.     for i in word:
  43.         if i == letter:
  44.             count += 1
  45.     print("Frequency of the letter " + letter + " in the given string is: " + str(count))
  46. # getLetterFrequency()
  47.  
  48.  
  49.  
  50. ####################################
  51. ##  4. FREQUENCY OF EVERY LETTER  ##
  52. ####################################
  53. def getEachLetterFrequency():
  54.     string = input("Enter a string: ")
  55.     dstring = ""
  56.     for i in string:
  57.         if i not in dstring:
  58.             dstring += i
  59.     for letter in dstring:
  60.         count = 0
  61.         for i in string:
  62.             if i == letter:
  63.                 count += 1
  64.         print("Frequency of the letter " + letter + " is: ", count)
  65. # getEachLetterFrequency()    
  66.  
  67.  
  68.  
  69. ##################################################
  70. ##  5. FREQUENCY OF A GIVEN WORD IN A SENTENCE  ##
  71. ##################################################
  72. def wordFrequency():
  73.     string = input("Enter a string: ")
  74.     string = string.split(" ")
  75.     tocheck = input("Enter the word: ")
  76.     count = 0
  77.     for word in string:
  78.         if word == tocheck:
  79.             count += 1
  80.     print("Frequency of the word " + tocheck + " is " + str(count))
  81. # wordFrequency()
  82.  
  83.  
  84.  
  85. ###########################
  86. ##  6. PALINDROME CHECK  ##
  87. ###########################
  88. def isPalindrome():
  89.     string = input("Enter a word: ")
  90.     i = 0
  91.     j = (len(string) - 1)
  92.     while (i <= (len(string) / 2)):
  93.         if string[i] == string[j]:
  94.             flag = 1
  95.         else:
  96.             flag = 0
  97.             break
  98.         i += 1
  99.         j -= 1
  100.     if flag:
  101.         print("YES! It is a palindrome")
  102.     else:
  103.         print("NO! Not a palindrome")
  104. #isPalindrome()
  105.  
  106.  
  107.  
  108. ################################################
  109. ##  7. FREQUENCY OF EVERY WORD IN A SENTENCE  ##
  110. ################################################
  111. def wordFrequency():
  112.     string = input("Enter a string: ")
  113.     dict = {}
  114.     dstring = []
  115.     for i in string.split(" "):
  116.         if i not in dstring:
  117.             dstring.append(i)
  118.     for word in dstring:
  119.         count = 0
  120.         for i in string.split(" "):
  121.             if i == word:
  122.                 count += 1
  123.         dict[word] = count
  124.         # print("Frequency of the word [" + word + "] is: ", count)
  125.     print("The required frequencies are: ")
  126.     print(dict)
  127. #wordFrequency()
  128.  
  129.  
  130.  
  131. ################################
  132. ##  8. SUBSTRING IN A STRING  ##
  133. ################################
  134. # ??? Split the mainstr into smaller strings of length equal to the substring's length
  135. # and chuck them into an array and then check if it exists.
  136. def isSubstring():
  137.     mainstring = input("Enter a string: ")
  138.     substring = input("Enter a substring: ")
  139.     sl = len(substring)
  140.     ml = len(mainstring)
  141.     splits = {}
  142.     flag = 0
  143.     for i in range (0, (ml - sl + 1)):
  144.         word = ""
  145.         for j in range(i, (i + sl)):
  146.             word += mainstring[j]
  147.             splits[i] = word            
  148.     for key, value in splits.items():
  149.         if value == substring:
  150.             flag = 1
  151.             position = key
  152.             break
  153.     if flag == 1:
  154.         print("SUCCESS!")
  155.         print("Given substring EXISTS in the given string starting from Index: " + str(position))
  156.     else:
  157.         print("ERROR!")
  158.         ("Substring DOES NOT EXIST in the given string")
  159. #isSubstring()
  160.  
  161.    
  162.    
Advertisement
Comments
Add Comment
Please, Sign In to add comment