Concept explainers
OCaml Code: Below is streams.ml and it needs the value "head". Please fix the code and make sure to test the code using various test cases to make sure the code works.
streams.ml
let rec even n =
if n = 0 then true
else odd (n - 1)
and odd n =
if n = 0 then false
else even (n - 1)
let rec squares n =
let rec squares_aux m acc =
if m = 0 then List.rev acc
else squares_aux (m - 1) (acc @ [m * m])
in
squares_aux n []
let rec fibs n =
let rec fibs_aux a b count acc =
if count = 0 then List.rev acc
else fibs_aux b (a + b) (count - 1) (a :: acc)
in
fibs_aux 0 1 n []
let rec evenFibs n =
let rec fibs_aux a b count acc =
if count = 0 then List.rev acc
else if a mod 2 = 0 then fibs_aux b (a + b) (count - 1) (a :: acc)
else fibs_aux b (a + b) count acc
in
fibs_aux 0 1 n []
let rec oddFibs n =
let rec fibs_aux a b count acc =
if count = 0 then List.rev acc
else if a mod 2 = 1 then fibs_aux b (a + b) (count - 1) (a :: acc)
else fibs_aux b (a + b) count acc
in
fibs_aux 1 1 n []
let rec is_prime n =
let rec is_not_divisible k =
k * k > n || (n mod k <> 0 && is_not_divisible (k + 1))
in
n > 1 && is_not_divisible 2
let rec primes n =
let rec find_primes k count acc =
if count = 0 then List.rev acc
else if is_prime k then find_primes (k + 1) (count - 1) (k :: acc)
else find_primes (k + 1) count acc
in
find_primes 2 n []
let rec rev_zip_diff l1 l2 f =
match l1, l2 with
| [], _ | _, [] -> []
| x :: xs, y :: ys -> (x, y, f (x, y)) :: rev_zip_diff xs ys f
let rec printGenList lst f =
match lst with
| [] -> ()
| x :: xs -> f x; printGenList xs f
let rec printList lst filename =
let oc = open_out filename in
let rec print_list_helper lst =
match lst with
| [] -> close_out oc
| x :: xs -> output_string oc (string_of_int x ^ " "); print_list_helper xs
in
print_list_helper lst
let rec printPairList lst filename =
let oc = open_out filename in
let rec print_pair_list_helper lst =
match lst with
| [] -> close_out oc
| (x, y) :: xs ->
output_string oc ("(" ^ string_of_int x ^ ", " ^ string_of_int y ^ ") ");
print_pair_list_helper xs
in
print_pair_list_helper lst
let rec take n lst =
if n <= 0 then []
else
match lst with
| [] -> []
| x :: xs -> x :: take (n - 1) xs
let string_of_tuple_list lst =
"[" ^ String.concat "; " (List.map (fun (x, y, z) -> "(" ^ string_of_int x ^ ", " ^ string_of_int y ^ ", " ^
string_of_int z ^ ")") lst) ^ "]"
let () =
let string_of_int_list lst = "[" ^ String.concat "; " (List.map string_of_int lst) ^ "]" in
print_endline ("even 2;;\n- : bool = " ^ string_of_bool (even 2));
print_endline ("even 3;;\n- : bool = " ^ string_of_bool (even 3));
print_endline ("odd 2;;\n- : bool = " ^ string_of_bool (odd 2));
print_endline ("odd 3;;\n- : bool = " ^ string_of_bool (odd 3));
print_endline ("take 10 squares;;\n- : int list = " ^ string_of_int_list (take 10 (squares 10)));
print_endline ("take 10 fibs;;\n- : int list = " ^ string_of_int_list (take 10 (fibs 10)));
print_endline ("take 10 evenFibs;;\n- : int list = " ^ string_of_int_list (take 10 (evenFibs 10)));
print_endline ("take 10 oddFibs;;\n- : int list = " ^ string_of_int_list (take 10 (oddFibs 10)));
print_endline ("take 10 primes;;\n- : int list = " ^ string_of_int_list (take 10 (primes 10)));
print_endline ("take 5 (rev_zip_diff evenFibs oddFibs (fun (x,y)->x-y));;\n- : (int * int * int) list = " ^
string_of_tuple_list (take 5 (rev_zip_diff (evenFibs 10) (oddFibs 10) (fun (x, y) -> x - y))));
print_endline "printGenList [\"how\"; \"the\"; \"turntables\"] (fun s -> print_string (s ^ \" \"));;";
printGenList ["how"; "the"; "turntables"] (fun s -> print_string (s ^ " "));
print_endline "\n-: unit = ()";
print_endline "printList [2; 4; 6; 8] \"printList.txt\";; (* \"2 4 6 8 \" *)";
printList [2; 4; 6; 8] "printList.txt";
print_endline "printPairList [(2, 1); (3, 2); (4, 3)] \"printPairList.txt\";; (* \"(2, 1) (3, 2) (4, 3) \" *)";
printPairList [(2, 1); (3, 2); (4, 3)] "printPairList.txt"
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 1 images
I ran the code and still got the same error. The code still needs the value head. Please fix the code and show screenshots of the correct code with the output. Make sure to use various test cases to test the code to make sure it works. Attached are images of the directions.
I ran the code and still got the same error. The code still needs the value head. Please fix the code and show screenshots of the correct code with the output. Make sure to use various test cases to test the code to make sure it works. Attached are images of the directions.
- Write a statement, using pyhton language, that creates a two-dimensional list with 5 rows and 3 columns. Then write nested loops that get an integer value from the user for each element in the list.arrow_forward10 - question You should use a Set instead of a List if you want to avoid duplicates (True/False)? a. True b. Falsearrow_forwardthis is my code so far but it its not accurate, not sure if i should def function and then for loop? import csv with open ('TopUni.csv', mode='r') as csv_file: csvReader = csv.reader(csv_file) line = [] for line in csvReader: sum(line) print(line)arrow_forward
- Modify task2.c so it will read from standard input and translate the contents just like the filter tr Modify this program so it work like trHint:if you run your program:./task2 abc ABCthen argv[1] is string "abc" argv[2] is the string "ABC" */ #include <stdio.h>#include <unistd.h>#include <string.h> int main(int argc, char *argv[]){ char c; while(1){ int r = read(0,&c,1); if(r==0) break; write(1,&c, 1); } return 0;}arrow_forward1c) Average sentence length We will create a function ( avg_sentence_len ) to calculate the average sentence length across a piece of text. This function should take text as an input parameter. Within this function: 1. sentences : Use the split() string method to split the input text at every '.'. This will split the text into a list of sentences. Store this in the variable sentences . To keep things simple, we will consider every "." as a sentence separator. (This decision could lead to misleading answers. For example, "Hello Dr. Jacob." is actually a single sentence, but our function will consider this 2 separate sentences). 2. words : Use the split() method to split the input text into a list of separate words, storing this in words . Again, to limit complexity, we will assume that all words are separated by a single space (" "). (So, while "I am going.to see you later" actually has 7 words, since there is no space after the ".", so we will assume the this to contain 6 separate…arrow_forwardThe code below prompts the user to enter their full name, and then converts the name to a list. Complete the code to output the person's initials. For example, if the user enters 'Adam Jones', then your code will output 'AJ'. If the user enters 'Adam Daniel Jones', then your code will output 'ADJ'. name = input('Enter your full name: ')nameList = name.split()nameListarrow_forward
- Write a code in python for this exercise belowarrow_forward•is Valid Phone Number(): ReturnsTrue if the provided phone number (represented as a string) is cor-rectly formatted, otherwiseFalse. To be considered correctly formatted, phone numbers must bewritten as###-###-####, where#is a digit between 0 and 9 .•validatePhoneBook(): A phone book is a list where each entry is a phone book record (represented asa dictionary; see below for more details). This function checks each phone book record in the phonebook for correctly formatted phone numbers.A phone book record is a dictionary which initially has two keys: the key"name"mapped to thecontact’s name (string) and the key"phone number"mapped to that contact’s phone number (alsoa string).validatePhoneBook()adds a new key"valid"to the record with the valueTrueif the phonenumber is formatted correctly, otherwiseFalse. 1. Write white-box and black-box tests for the function is ValidPhoneNumber(). You should do this with-out Python, either on paper, or in a simple document. Don’t worry about…arrow_forwardPython code. Please write your own code. Thank you!arrow_forward
- I need help with the last two functions in pythonarrow_forwardPOEM_LINE = str POEM = List[POEM_LINE] PHONEMES = Tuple[str] PRONUNCIATION_DICT = Dict[str, PHONEMES] def all_lines_rhyme(poem_lines: POEM, lines_to_check: List[int], word_to_phonemes: PRONUNCIATION_DICT) -> bool: """Return True if and only if the lines from poem_lines with index in lines_to_check all rhyme, according to word_to_phonemes. Precondition: lines_to_check != [] >>> poem_lines = ['The mouse', 'in my house', 'electric.'] >>> lines_to_check = [0, 1] >>> word_to_phonemes = {'THE': ('DH', 'AH0'), ... 'MOUSE': ('M', 'AW1', 'S'), ... 'IN': ('IH0', 'N'), ... 'MY': ('M', 'AY1'), ... 'HOUSE': ('HH', 'AW1', 'S'), ... 'ELECTRIC': ('IH0', 'L', 'EH1', 'K', ... 'T', 'R', 'IH0', 'K')} >>> all_lines_rhyme(poem_lines, lines_to_check, word_to_phonemes) True…arrow_forwardPYTHON Complete the function below, which takes two arguments: data: a list of tweets search_words: a list of search phrases The function should, for each tweet in data, check whether that tweet uses any of the words in the list search_words. If it does, we keep the tweet. If it does not, we ignore the tweet. data = ['ZOOM earnings for Q1 are up 5%', 'Subscriptions at ZOOM have risen to all-time highs, boosting sales', "Got a new Mazda, ZOOM ZOOM Y'ALL!", 'I hate getting up at 8am FOR A STUPID ZOOM MEETING', 'ZOOM execs hint at a decline in earnings following a capital expansion program'] Hint: Consider the example_function below. It takes a list of numbers in numbers and keeps only those that appear in search_numbers. def example_function(numbers, search_numbers): keep = [] for number in numbers: if number in search_numbers(): keep.append(number) return keep def search_words(data, search_words):arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education