Phone

+919997782184

Email

support@roboticswithpython.com

Geeks of Coding

Join us on Telegram

Viewing 2 reply threads
  • Author
    Posts
    • #959
      Abhishek TyagiAbhishek Tyagi
      Keymaster

      1. Given a list of filenames, we want to rename all the files with the extension hpp to the extension h by generating a list of tuples of the form (old_name, new_name).
      That is, given the following list of filenames

      filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
      complete the starter code to generate the following newfilenames list of tuples
      newfilenames = [('program.c', 'program.c'), ('stdio.hpp', 'stdio.h'), ('sample.hpp', 'sample.h'), ('a.out', 'a.out'), ('math.hpp', 'math.h'), ('hpp.out', 'hpp.out')]

      Answer-

      filenames = ["program.c", "stdio.hpp", "sample.hpp", "a.out", "math.hpp", "hpp.out"]
      newfilenames = []
      for oldfilename in filenames:
        if ".hpp" in oldfilename:
          newfilenames.append((oldfilename,oldfilename.replace(".hpp",".h")))
        else:
          newfilenames.append((oldfilename,oldfilename))
      
      print (newfilenames) # Should be [('program.c', 'program.c'), ('stdio.hpp', 'stdio.h'), ('sample.hpp', 'sample.h'), ('a.out', 'a.out'), ('math.hpp', 'math.h'), ('hpp.out', 'hpp.out')]

      2. Let’s create a function that turns text into pig latin: a simple text transformation that modifies each word moving the first character to the end and appending “ay” to the end. For example, python ends up as ythonpay.

      Answer-

      def pig_latin(text):
        say = ""
        # Separate the text into words
        words = text.split()
        for word in words:
          # Create the pig latin word and add it to the list
          texts = word[1:]+word[0]+ "ay" + " "
          say += texts
          # Turn the list back into a phrase
        return say
      		
      print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
      print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"

      3. The permissions of a file in a Linux system are split into three sets of three permissions:
      read, write, and execute for the owner, group, and others. Each of the three values can be expressed as an octal number summing each permission, with 4 correspondings to read, 2 to write, and 1 to execute.
      Or it can be written with a string using the letters r, w, and x or – when the permission is not granted. For example 640 is read/write for the owner, read for the group, and no permissions for the others; converted to a string, it would be: “rw-r—–” 755 is read/write/execute for the owner, and read/execute for group and others; converted to a string, it would be: “rwxr-xr-x” Fill in the blanks to make the code convert permission in octal format into a string format.

      Answer-

      def octal_to_string(octal):
          result = ""
          value_letters = [(4,"r"),(2,"w"),(1,"x")]
          # Iterate over each of the digits in octal
          for digit in [int(n) for n in str(octal)]:
              # Check for each of the permissions values
              for value, letter in value_letters:
                  if digit >= value:
                      result += letter
                      digit -= value
                  else:
                      result+="-"
          return result
      print(octal_to_string(755)) # Should be rwxr-xr-x
      print(octal_to_string(644)) # Should be rw-r--r--
      print(octal_to_string(750)) # Should be rwxr-x---
      print(octal_to_string(600)) # Should be rw-------

      4. Tuples and lists are very similar types of sequences. What is the main thing that makes a tuple different from a list?

      Answer-
      A tupple is immutable




      5. The group_list function accepts a group name and a list of members, and returns a string with the format: group_name: member1, member2, … For example, group_list(“g”, [“a”,”b”,”c”]) returns “g: a, b, c”. Fill in the gaps in this function to do that.

      Answer-

      def group_list(group, users):
        result = ""
        if len(group)!=0:
          result += group + ":"
        if len(group)==0:
          result += " " + users[0]
        if len(group)>0:
          for x in users[1:]:
            result += "," + x
        return result
      print(group_list("Marketing", ["Mike", "Karen", "Jake", "Tasha"])) # Should be "Marketing: Mike, Karen, Jake, Tasha"
      print(group_list("Engineering", ["Kim", "Jay", "Tom"])) # Should be "Engineering: Kim, Jay, Tom"
      print(group_list("Users", "")) # Should be "Users:"

      6. The guest_list function reads in a list of tuples with the name, age, and profession of each party guest, and prints the sentence “Guest is X years old and works as __.” for each one. For example, guest_list((‘Ken’, 30, “Chef”), (“Pat”, 35, ‘Lawyer’), (‘Amanda’, 25, “Engineer”)) should print out: Ken is 30 years old and works as Chef. Pat is 35 years old and works as Lawyer. Amanda is 25 years old and works as Engineer. Fill in the gaps in this function to do that.

      Answer-

      def guest_list(guests):
      	count = 0
      	if count < 3:
      		for guest in guests:
      			name, age, job = guest
      			print("{} is {} years old and works as {}".format(name, age, job))
      			count = count + 1
      guest_list([('Ken', 30, "Chef"), ("Pat", 35, 'Lawyer'), ('Amanda', 25, "Engineer")])
      
      """
      Output should match:
      Ken is 30 years old and works as Chef
      Pat is 35 years old and works as Lawyer
      Amanda is 25 years old and works as Engineer
      """
    • #1304
      adlai hamptonadlai hampton
      Participant

      the questions of 1,3,5 and 6 aren’t success.

    • #1320
      Bayo BelloBayo Bello
      Participant

      1. The entry for question one above produces a wrong answer.
      The correct entry should be:

      newfilenames = []
      for oldfilename in filenames:
      if “.hpp” in oldfilename:
      newfilenames.append(oldfilename.replace(“.hpp”, “.h”))
      else:
      newfilenames.append(oldfilename)

      print(newfilenames)
      # Should be [“program.c”, “stdio.h”, “sample.h”, “a.out”, “math.h”, “hpp.out”]

      Result = [‘program.c’, ‘stdio.h’, ‘sample.h’, ‘a.out’, ‘math.h’, ‘hpp.out’]

      6. The entry for question 6 can be simplified using
      for guest in guests:
      name, age, job = guest
      print(“{} is {} years old and works as {}”.format(name, age, job))

Viewing 2 reply threads
  • You must be logged in to reply to this topic.