aishwaryaneelakandan / Assignment

1 stars 1 forks source link

task1005 #11

Open ravi-priya opened 3 years ago

ravi-priya commented 3 years ago

10/05 assignment

Slot 1 write these methods in Compare class( CompareAgain5.java) and a test for each in CompareAgain5Test.

1.Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that value, and set the key "a" to have the value "". Basically "b" is a bully, taking the value and replacing it with the empty string.

mapBully({"a": "candy", "b": "dirt"}) → {"a": "", "b": "candy"} mapBully({"a": "candy"}) → {"a": "", "b": "candy"} mapBully({"a": "candy", "b": "carrot", "c": "meh"}) → {"a": "", "b": "candy", "c": "meh"}

2.Given a map of food keys and topping values, modify and return the map as follows: if the key "ice cream" is present, set its value to "cherry". In all cases, set the key "bread" to have the value "butter".

topping1({"ice cream": "peanuts"}) → {"bread": "butter", "ice cream": "cherry"} topping1({}) → {"bread": "butter"} topping1({"pancake": "syrup"}) → {"bread": "butter", "pancake": "syrup"}

3.Modify and return the given map as follows: if the keys "a" and "b" are both in the map and have equal values, remove them both.

mapAB2({"a": "aaa", "b": "aaa", "c": "cake"}) → {"c": "cake"} mapAB2({"a": "aaa", "b": "bbb"}) → {"a": "aaa", "b": "bbb"} mapAB2({"a": "aaa", "b": "bbb", "c": "aaa"}) → {"a": "aaa", "b": "bbb", "c": "aaa"}

4.Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that same value. In all cases remove the key "c", leaving the rest of the map unchanged.

mapShare({"a": "aaa", "b": "bbb", "c": "ccc"}) → {"a": "aaa", "b": "aaa"} mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"} mapShare({"a": "aaa", "c": "meh", "d": "hi"}) → {"a": "aaa", "b": "aaa", "d": "hi"}

5.Given a map of food keys and their topping values, modify and return the map as follows: if the key "ice cream" has a value, set that as the value for the key "yogurt" also. If the key "spinach" has a value, change that value to "nuts".

topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"} topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"} topping2({"yogurt": "salt"}) → {"yogurt": "salt"}

6.Modify and return the given map as follows: if exactly one of the keys "a" or "b" has a value in the map (but not both), set the other to have that same value in the map.

mapAB3({"a": "aaa", "c": "cake"}) → {"a": "aaa", "b": "aaa", "c": "cake"} mapAB3({"b": "bbb", "c": "cake"}) → {"a": "bbb", "b": "bbb", "c": "cake"} mapAB3({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "aaa", "b": "bbb", "c": "cake"}

7.Modify and return the given map as follows: for this problem the map may or may not contain the "a" and "b" keys. If both keys are present, append their 2 string values together and store the result under the key "ab".

mapAB({"a": "Hi", "b": "There"}) → {"a": "Hi", "ab": "HiThere", "b": "There"} mapAB({"a": "Hi"}) → {"a": "Hi"} mapAB({"b": "There"}) → {"b": "There"}

8.Given a map of food keys and topping values, modify and return the map as follows: if the key "potato" has a value, set that as the value for the key "fries". If the key "salad" has a value, set that as the value for the key "spinach".

topping3({"potato": "ketchup"}) → {"potato": "ketchup", "fries": "ketchup"} topping3({"potato": "butter"}) → {"potato": "butter", "fries": "butter"} topping3({"salad": "oil", "potato": "ketchup"}) → {"spinach": "oil", "salad": "oil", "potato": "ketchup", "fries": "ketchup"}

9.Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.

mapAB4({"a": "aaa", "b": "bb", "c": "cake"}) → {"a": "aaa", "b": "bb", "c": "aaa"} mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"} mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}

10.Given an array of strings, return a Map<String, Integer> containing a key for every different string in the array, always with the value 0. For example the string "hello" makes the pair "hello":0. We'll do more complicated counting later, but for this problem the value is simply 0.

word0(["a", "b", "a", "b"]) → {"a": 0, "b": 0} word0(["a", "b", "a", "c", "b"]) → {"a": 0, "b": 0, "c": 0} word0(["c", "b", "a"]) → {"a": 0, "b": 0, "c": 0}

Slot 2 write these methods in Compare class( CompareAgain5.java) and a test for each in CompareAgain5Test.

11.The classic word-count algorithm: given an array of strings, return a Map<String, Integer> with a key for each different string, with the value the number of times that string appears in the array.

wordCount(["a", "b", "a", "c", "b"]) → {"a": 2, "b": 2, "c": 1} wordCount(["c", "b", "a"]) → {"a": 1, "b": 1, "c": 1} wordCount(["c", "c", "c", "c"]) → {"c": 4}

12.Given an array of strings, return a Map<String, Boolean> where each different string is a key and its value is true if that string appears 2 or more times in the array.

wordMultiple(["a", "b", "a", "c", "b"]) → {"a": true, "b": true, "c": false} wordMultiple(["c", "b", "a"]) → {"a": false, "b": false, "c": false} wordMultiple(["c", "c", "c", "c"]) → {"c": true}

13.Given an array of strings, return a Map<String, Integer> containing a key for every different string in the array, and the value is that string's length.

wordLen(["a", "bb", "a", "bb"]) → {"bb": 2, "a": 1} wordLen(["this", "and", "that", "and"]) → {"that": 4, "and": 3, "this": 4} wordLen(["code", "code", "code", "bug"]) → {"code": 4, "bug": 3}

14.Given an array of non-empty strings, return a Map<String, String> with a key for every different first character seen, with the value of all the strings starting with that character appended together in the order they appear in the array.

firstChar(["salt", "tea", "soda", "toast"]) → {"s": "saltsoda", "t": "teatoast"} firstChar(["aa", "bb", "cc", "aAA", "cCC", "d"]) → {"a": "aaaAA", "b": "bb", "c": "cccCC", "d": "d"} firstChar([]) → {}

15.We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and then return the given array of non-empty strings as follows: if a string matches an earlier string in the array, swap the 2 strings in the array. When a position in the array has been swapped, it no longer matches anything. Using a map, this can be solved making just one pass over the array. More difficult than it looks.

allSwap(["ab", "ac"]) → ["ac", "ab"] allSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "azz", "aaa"] allSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "aj", "ai", "by", "bx"]

16.Given an array of non-empty strings, create and return a Map<String, String> as follows: for each string add its first character as a key with its last character as the value.

pairs(["code", "bug"]) → {"b": "g", "c": "e"} pairs(["man", "moon", "main"]) → {"m": "n"} pairs(["man", "moon", "good", "night"]) → {"g": "d", "m": "n", "n": "t"}

17.Loop over the given array of strings to build a result string like this: when a string appears the 2nd, 4th, 6th, etc. time in the array, append the string to the result. Return the empty string if no string appears a 2nd time.

wordAppend(["a", "b", "a"]) → "a" wordAppend(["a", "b", "a", "c", "a", "d", "a"]) → "aa" wordAppend(["a", "", "a"]) → "a"

18.We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and then return the given array of non-empty strings as follows: if a string matches an earlier string in the array, swap the 2 strings in the array. A particular first char can only cause 1 swap, so once a char has caused a swap, its later swaps are disabled. Using a map, this can be solved making just one pass over the array. More difficult than it looks.

firstSwap(["ab", "ac"]) → ["ac", "ab"] firstSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "aaa", "azz"] firstSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "ai", "aj", "bx", "by"]

19.Given a list of integers, return a list where each integer is multiplied by 2.

doubling([1, 2, 3]) → [2, 4, 6] doubling([6, 8, 6, 8, -1]) → [12, 16, 12, 16, -2] doubling([]) → []

20.Given a list of strings, return a list where each string is replaced by 3 copies of the string concatenated together.

copies3(["a", "bb", "ccc"]) → ["aaa", "bbbbbb", "ccccccccc"] copies3(["24", "a", ""]) → ["242424", "aaa", ""] copies3(["hello", "there"]) → ["hellohellohello", "theretherethere"]

Slot 3 write these methods in Compare class( CompareAgain5.java) and a test for each in CompareAgain5Test.

21.Given a list of non-negative integers, return an integer list of the rightmost digits. (Note: use %)

rightDigit([1, 22, 93]) → [1, 2, 3] rightDigit([16, 8, 886, 8, 1]) → [6, 8, 6, 8, 1] rightDigit([10, 0]) → [0, 0]

22.Given a list of integers, return a list where each integer is multiplied with itself.

square([1, 2, 3]) → [1, 4, 9] square([6, 8, -6, -8, 1]) → [36, 64, 36, 64, 1] square([]) → []

23.Given a list of strings, return a list where each string has "y" added at its start and end.

moreY(["a", "b", "c"]) → ["yay", "yby", "ycy"] moreY(["hello", "there"]) → ["yhelloy", "ytherey"] moreY(["yay"]) → ["yyayy"]

24.Given a list of strings, return a list where each string is converted to lower case (Note: String toLowerCase() method).

lower(["Hello", "Hi"]) → ["hello", "hi"] lower(["AAA", "BBB", "ccc"]) → ["aaa", "bbb", "ccc"] lower(["KitteN", "ChocolaTE"]) → ["kitten", "chocolate"]

25.Given a list of strings, return a list where each string has "*" added at its end.

addStar(["a", "bb", "ccc"]) → ["a", "bb", "ccc"] addStar(["hello", "there"]) → ["hello", "there"] addStar([""]) → ["**"]

26.Given a list of integers, return a list where each integer is added to 1 and the result is multiplied by 10.

math1([1, 2, 3]) → [20, 30, 40] math1([6, 8, 6, 8, 1]) → [70, 90, 70, 90, 20] math1([10]) → [110]

27.Given a list of strings, return a list where each string has all its "x" removed.

noX(["ax", "bb", "cx"]) → ["a", "bb", "c"] noX(["xxax", "xbxbx", "xxcx"]) → ["a", "bb", "c"] noX(["x"]) → [""]

28.Given a list of integers, return a list of the integers, omitting any that are less than 0.

noNeg([1, -2]) → [1] noNeg([-3, -3, 3, 3]) → [3, 3] noNeg([-1, -1, -1]) → []

29.Given a list of strings, return a list of the strings, omitting any string that contains a "z". (Note: the str.contains(x) method returns a boolean)

noZ(["aaa", "bbb", "aza"]) → ["aaa", "bbb"] noZ(["hziz", "hzello", "hi"]) → ["hi"] noZ(["hello", "howz", "are", "youz"]) → ["hello", "are"]

30.Given a list of strings, return a list where each string has "y" added at its end, omitting any resulting strings that contain "yy" as a substring anywhere.

noYY(["a", "b", "c"]) → ["ay", "by", "cy"] noYY(["a", "b", "cy"]) → ["ay", "by"] noYY(["xx", "ya", "zz"]) → ["xxy", "yay", "zzy"]

aishwaryaneelakandan commented 3 years ago

Slot 1 https://github.com/aishwaryaneelakandan/Assignment/tree/master/Assignment/src Chitappa can u say me am I doing it right

ravi-priya commented 3 years ago

continue doing it..

aishwaryaneelakandan commented 3 years ago

https://github.com/aishwaryaneelakandan/Assignment/tree/master/Assignment/src