Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In addition to any functions listed for each exercise, you may always use the fo

ID: 3728687 • Letter: I

Question

In addition to any functions listed for each exercise, you may always use the following:

Tuple construction & component access: (), fst, snd

List construction & access: [], (:), head, tail, init, last, (!!), (++)

Boolean operators: not, (&&), (||)

Composition: (.)

Arithmetic: (+), (-), (/), (*)

Comparison: (==), (/=), (<), (<=), (>), (>=)

HOFs: map, foldr, foldl

Implement the following functions using either a left or right fold. For these functions, it's likely that you'll need to pass a tuple into the fold, and extract a value from the (tuple) returned by the fold to return as a result. Endeavor to use point-free style.

gap :: (Eq a) => a -> a -> [a] -> Maybe Int

Returns the integer distance between first appearance of two elements in a list.

Examples:

> gap 3 8 [1..10]

Just 5

> gap 8 3 [1..10]

Nothing

> gap 'h' 'l' "hello"

Just 2

> gap 'h' 'z' "hello"

Nothing

evalExpr :: String -> Int

Evaluates a string containing a simple arithmetic expression using only single digit operands and (binary) + and - operators.

Examples:

words' :: String -> [String]

Returns the space-delineated words found in a string as a list.

Examples:

dropWhile' :: (a -> Bool) -> [a] -> [a]

Removes elements from the beginning of a list that satisfy the predicate.

Examples:

join :: a -> [[a]] -> [a]

Write a function that joins lists together using a separator value --- the separator should only appear between elements, and not at the end.

Examples:

> join '-' ["hello", "how", "are", "you"] "hello-how-are-you" > join 0 [[1], [2,3]] [1,0,2,3]

Permitted functions: (++), (:)

unzip' :: [(a,b)] -> ([a], [b])

Write a function that takes a list of two-tuples and returns a tuple of two lists -- the first containing the first element of each tuple, and the second the second element (the reverse of "zip").

Examples:

Permitted functions: (:)

runLengthEncode :: String -> [(Int,Char)]

Run-length encoding is a simple form of data compression that replaces characters in a stream with the count of adjacent occurrences of that character and just a single instance of the character itself. Write a function that takes a string and returns a list of tuples reprenting the run-length encoding of that string.

Examples:

Permitted functions: (:), (==), (+)

runLengthDecode :: [(Int,Char)] -> String

Write a function that takes the output of runLengthEncode and returns the original string.

Examples:

Permitted functions: (++), (:), replicate

vigenere :: String -> String -> String

The Vigenere encryption scheme is similar to the Caesar cipher presented in class in that it makes use of shifting, but instead of a single numeric key applied uniformly to the entire plain text, a string of characters is used as the key. The numeric value of each character (as its position in the alphabet) is used as a shift value, and if the key is shorter than the length of the plain text it is simply repeated.

E.g., to encrypt the plain text "FOOBAR" with the key "BAZ", we can proceed as follows:

Pair each letter of the plain text with a letter from the key:

Convert each letter to its numeric value (A=0, B=1 ... Z=25)

Add them together:

"Wrap" the numbers around so they're in the range 0-25:

Convert the numbers back into letters:

Write a function that takes a key and plain text and returns the Vigenere encrypted cipher text. Plain text can contain a mix of lowercase and uppercase letters and punctuation, but all letters will be interpreted as uppercase. Punctuation will not be encrypted. The key will contain only letters (lower or upper case), but again will only be interpreted as uppercase.

Examples:

> vigenere "baz" "foobar"

"GONCAQ"

>vigenere "Yadda" "Hello, world!"

"FEOOO, UOUOD!"

Permitted functions: (+), (-), (:), (++), (&&), rem, ord, chr, isLetter, toUpper (to use the last four you'll need to import Data.Char)

Explanation / Answer

Returns the integer distance between the first appearance of two elements in a list.

package com.chegg.service;

public class FindSum {

public static void main(String args[]) {

int a = 3;

int b = 8;

int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

String result = gap(a, b, arr);

System.out.println(result);

char c = 'h';

char d = 'l';

char arr1[] = { 'h', 'e', 'l', 'l', 'o' };

String result1 = gap(c, d, arr1);

System.out.println(result1);

char e = 'h';

char f = 'z';

String result2 = gap(e, f, arr1);

System.out.println(result2);

}

public static String gap(int a, int b, int[] arr) {

int startIndex = 0;

int endIndex = 0;

for (int i = 0; i < arr.length; i++) {

if (arr[i] == a) {

startIndex = i;

break;

}

}

for (int i = 0; i < arr.length; i++) {

if (arr[i] == b) {

endIndex = i;

break;

}

}

if (endIndex - startIndex > 0) {

return Integer.toString(endIndex - startIndex);

}

return "Nothing";

}

public static String gap(char a, char b, char[] arr) {

int startIndex = 0;

int endIndex = 0;

for (int i = 0; i < arr.length; i++) {

if (arr[i] == a) {

startIndex = i;

break;

}

}

for (int i = 0; i < arr.length; i++) {

if (arr[i] == b) {

endIndex = i;

break;

}

}

if (endIndex - startIndex > 0) {

return Integer.toString(endIndex - startIndex);

}

return "Nothing";

}

}

Evaluates a string containing a simple arithmetic expression using only single digit operands and (binary) + and - operators.

package com.chegg.service;

import java.util.Stack;

public class FindSum {

public static void main(String args[]) {

int result = evaluateString("1+2+5");

System.out.println(result);

}

public static int get(char op, int a, int b) {

switch (op) {

case '+':

return a + b;

case '-':

return b - a;

}

return 0;

}

@SuppressWarnings({ "unchecked", "rawtypes" })

public static int evaluateString(String str) {

char[] arr = str.toCharArray();

Stack<Integer> numbers = new Stack();

Stack<Character> operands = new Stack();

for (int i = 0; i < arr.length; i++) {

if (arr[i] >= '0' && arr[i] <= '9') {

StringBuffer s = new StringBuffer();

while (i < arr.length && arr[i] >= '0' && arr[i] <= '9') {

s.append(arr[i++]);

}

--i;

numbers.push(Integer.parseInt(s.toString()));

} else if (arr[i] == '+' || arr[i] == '-') {

while (!operands.empty()) {

int a = get(operands.pop(), numbers.pop(), numbers.pop());

numbers.push(a);

}

operands.push(arr[i]);

}

}

while (!operands.empty()) {

int a = get(operands.pop(), numbers.pop(), numbers.pop());

numbers.push(a);

}

return numbers.pop();

}

}

Returns the space-delineated words found in a string as a list.

package com.chegg.service;

import java.util.Stack;

public class FindSum {

public static void main(String args[]) {

String[] result = spaceDelineated("hello how are you");

for(String str: result)

System.out.print(str);

}

public static String[] spaceDelineated(String str) {

String[] arr= str.split(" ");

return arr;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote