Question

In: Computer Science

PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s...

PYTHON CODE

- Write the body of a function second_instance(s, c) which consumes a string s and a length 1 string c that is contained at least twice in s and returns the index of the second location of c.

second_instance: Str Str -> Nat
Requires:
len(c) == 1
c occurs at least twice in s
  
Examples:
second_instance("banana", "a") => 3
second_instance("bb", "b") => 1

- Write the body of a function make_list(n) which consumes a natural number n and returns a list of strings where in position , the number  is repeated as a string  times. You must use loops in your solution. Do not use recursion or abstract list functions.

Returns the list of strings formed by in position i,
repeating i a total of i times
  
make_list: Nat -> (listof Str)
  
Examples:
make_list(0) => ['']
make_list(3) => ['', '1', '22', '333']

- Write the body of a function niven(n) using recursion that consumes a natural number n and returns True if and only if n is a Niven number and False otherwise. A Niven number is a number which is divisible by the sum of its digits.

Returns True if and only if n is a Niven number.
  
niven: Nat -> Bool
  
Examples:
niven(0) => False
niven(1) => True
niven(132) => True
niven(143) => False

- Write the body of a function find_max(L) using loops that returns the maximum of a non-empty list of integers L. Do not use recursion, abstract list functions, or the command max.

Returns the maximum in a non-empty list of integers L
  
find_max: (listof Int) -> Int
  
Examples:
find_max([1]) => 1
find_max([-10,-1,-5]) => -1

Solutions

Expert Solution

program plan:

second_instance():

  • set count = 0
  • loop for i = 0 to range(len(s))
    • if s[i] == c
      • increment count
      • if count == 2
        • return i

make_list():

  • set L = ['']*(n+1)
  • loop for i = 1 to n
    • set L[i] str(i)*i

nivel(n):

  • set total = 0 and n1 = n
  • while n1>0
    • add n1%10 to total
    • set n = n/10
  • if n%total = 0 return true, else return false

find_max()

  • set max = L[0]
  • loop through list
    • if current element is greater than max, then set max = current element

program:

def second_instance(s, c):
count = 0
for i in range(len(s)):
if c == s[i]:
count+=1
if count == 2:
return i

def make_list(n):
l = ['']*(n+1)
for i in range(1,n+1):
l[i] = str(i)*i

return l

def niven(n):
total = 0
n1 = n
while n1>0:
total += n1%10
n1 = n1//10
return n%total == 0

def find_max(L):
m = L[0]
for x in L:
if m < x:
m = x
return m


print(second_instance("banana","a"))
print(make_list(3))
print(niven(18))
print(find_max([-10,-1,-5]))

output:


Related Solutions

PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of natural numbers L and return the single digit that occurs most frequently at the end of the numbers in the list. The function returns the smallest digit in the case of a tie. Your function should run in O(n) time. Do not mutate the passed parameter. def most_ending_digit(L) ''' Returns the single digit that occurs most frequently as the last digit of numbers in...
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Use the Design Recipe to write a function count_vowels, which consumes a string and returns the...
Use the Design Recipe to write a function count_vowels, which consumes a string and returns the number of vowels in that string. For these purposes, the vowels are a, e, i, o, and u, but never y. Include a Docstring! Note: Count both upper and lowercase vowels! Write 3 assert_equal statements to test your function.
Code in Python 1. A function named get_score has been defined which consumes no parameters and...
Code in Python 1. A function named get_score has been defined which consumes no parameters and returns an int value. Call get_score and print the result. 2.A function named area_rectangle has been defined which consumes two parameters that are both float values and returns a float value. Call area_rectangle with arguments of variables named length and width and print the result. 3. Assume a function called calculate_cone_volume is already defined. The function has 2 parameters: height and radius (in that...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or...
Python. Write a function last_occur(s, e) that takes as inputs a sequence (i.e., a string or list) s and an element e, and that calls itself recursively to find and return the index of the last occurrence of e in s. If s is a string, e will be a single-character string; if s is a list, e can be any value. Don’t forget that the index of the first element in a sequence is 0. Important notes: If e...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
Write a python code to Design and implement a function with no input parameter which reads...
Write a python code to Design and implement a function with no input parameter which reads a number from input (like 123). Only non-decimal numbers are valid (floating points are not valid). The number entered by the user should not be divisible by 10 and if the user enters a number that is divisible by 10 (like 560), it is considered invalid and the application should keep asking until the user enters a valid input. Once the user enters a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT