Difference between revisions of "Activity assignment 1"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
== Activity assignments ==  
 
== Activity assignments ==  
  
Implement following functions. There will be 5 points for each of these functions.
+
Implement following functions.  
  
* Create a function that gets a list of tuples (Int, Char). This function takes these pair and for each of these pairs (n, a) generates a string where  
+
* Create a function that gets a list of tuples - <code>[(Int, Char)]</code>. This function takes these pairs and for each of these pairs <code>(n, a)</code> generates a string where the character <code>a</code> is multiplied <code>n</code> times. The function result is a concatenation of these sub-strings.  
  the character <code>a</code> is multiplied <code>n</code> times. The function result is a concatenation of these sub-strings.  
 
 
 
<syntaxhighlight lang="Haskell" >
 
<syntaxhighlight lang="Haskell" >
 
create :: [(Int, Char)] -> String
 
create :: [(Int, Char)] -> String
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark" >
 +
create [(2,'a'), (3,'B')] = "aaBBB"
 +
</syntaxhighlight>
 +
 +
* Create a function that takes two strings - <code>a</code> and <code>b</code>. It will print how many times the string <code>a</code> contains the string <code>b</code>.
 +
<syntaxhighlight lang="Haskell" >
 +
count :: String -> String -> Int
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark" >
 +
count "AbcAbcabcAbc" "Abc" = 3
 +
</syntaxhighlight>
 +
 +
* Create a function that gets a string <code>line</code> and a list of pairs - <code>[(Int,Int)]</code>. Each of these pairs contain two indexes (starting from 0) ant it describes a swap between two positions in the original string <code>line</code>. Function returns the original string where all these swaps have been applied.
 +
<syntaxhighlight lang="Haskell" >
 +
swapIt :: String -> [(Int,Int)] -> String
 +
</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark" >
 +
swapIt "ABC" [(0,2)] = "CBA"
 +
swapIt "ABCD" [(0,2), (1,3),(0,1),(2,3)] = "DCBA"
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
* Create a function, that gets two lists of integers. The first list is a lit of indexes to the second argument (starting from 1). The result will be the sum of elements from the second list at positions defined by the first list.
 +
<syntaxhighlight lang="Haskell" >
 +
sumIt :: [Int] -> [Int] -> Int
 +
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
create [(2,'a'), (3,'B')] = "aaBBB"
+
sumIt [3,4,2] [5,6,7,8] = 21
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 13:16, 20 August 2020

Activity assignments

Implement following functions.

  • Create a function that gets a list of tuples - [(Int, Char)]. This function takes these pairs and for each of these pairs (n, a) generates a string where the character a is multiplied n times. The function result is a concatenation of these sub-strings.
create :: [(Int, Char)] -> String
create [(2,'a'), (3,'B')] = "aaBBB"
  • Create a function that takes two strings - a and b. It will print how many times the string a contains the string b.
count :: String -> String -> Int
count "AbcAbcabcAbc" "Abc" = 3
  • Create a function that gets a string line and a list of pairs - [(Int,Int)]. Each of these pairs contain two indexes (starting from 0) ant it describes a swap between two positions in the original string line. Function returns the original string where all these swaps have been applied.
swapIt :: String -> [(Int,Int)] -> String
swapIt "ABC" [(0,2)] = "CBA"
swapIt "ABCD" [(0,2), (1,3),(0,1),(2,3)] = "DCBA"
  • Create a function, that gets two lists of integers. The first list is a lit of indexes to the second argument (starting from 1). The result will be the sum of elements from the second list at positions defined by the first list.
sumIt :: [Int] -> [Int] -> Int
sumIt [3,4,2] [5,6,7,8] = 21