Aung Wai Yan Hein 1 year ago
parent
commit
ea451bc11f

+ 22 - 0
loops2/explanations/explanation0.html

@@ -0,0 +1,22 @@
+<div>
+<p><strong>Explanation</strong></p>
+<p>1. Think about what is given.<p>
+<p>Given: list containing 0 and 1 </p>
+<p>2. What to find: return flipped values in the list </p>
+<p>3. Steps needed:</p>
+<ul>
+    <li>
+        <p>initialize variable new_lst to store list of flipped values </p>
+    </li>
+    <li>
+        <p>iterate over the list</p>
+    </li>
+    <li>
+        <p>append the flipped values to the new list</p>
+        <p>to flip the value from 0 to 1 (or) 1 to 0, we can subtract the value that we want to flip from 1 [in other words: 1-num]</p>
+    </li>
+    <li>
+        <p>return new_lst</p>
+    </li>
+</ul>
+</div>

+ 25 - 0
loops2/explanations/explanation1.html

@@ -0,0 +1,25 @@
+<div>
+<p><strong>Explanation</strong></p>
+<p>1. Think about what is given.<p>
+<p>Given: Value, list </p>
+<p>2. What to find: whether the value is in the list </p>
+<p>3. Steps needed:</p>
+<ul>
+    <li>
+        <p>iterate over the big list using "for loop"</p>
+    </li>
+    <li>
+        <p>iterate again over the small list using "for loop" inside the first "for loop" [nested loop]</p>
+    </li>
+    <li>
+        <p>Then compare the <strong>given value</strong> with <strong>each value in the list</strong> [use "==" sign] </p>
+    </li>
+    <li>
+        <p>if the value matches, assign <strong>"True"</strong> to variable <strong>"found"</strong></p>
+        <p>["found = True"]</p>
+    </li>
+    <li>
+        <p>return "found"</p>
+    </li>
+</ul>
+</div>

+ 23 - 0
loops2/explanations/explanation4.html

@@ -0,0 +1,23 @@
+<div>
+<p><strong>Explanation</strong></p>
+<p>1. Think about what is given.<p>
+<p>Given: string</p>
+<p>2. What to find: return string without extra spaces (meaning: just with single space) </p>
+<p>3. Steps needed:</p>
+<ul>
+    <li>
+        <p>initialize variables(last_char and new_string) to store the last character and new string</p>
+    </li>
+    <li>
+        <p>iterate over the string using 'for loop'</p>
+    </li>
+    <li>
+        <p>check whether the last character and current character already have single space</p>
+        <p>if true, remove spaces </p>
+        <p>if false, update the last character to the current character and update new string</p>
+    </li>
+    <li>
+        <p>return new_string</p>
+    </li>
+</ul>
+</div>

+ 29 - 0
loops2/explanations/explanation5.html

@@ -0,0 +1,29 @@
+<div>
+<p><strong>Explanation</strong></p>
+<p>1. Think about what is given.<p>
+<p>Given: list of high_temp, list of low_temp </p>
+<p>2. What to find: average of daily temperature </p>
+<p>3. Steps needed:</p>
+<ul>
+    <li>
+        <p>create an empty new_list</p>
+    </li>
+    <li>
+        <p>iterate over the indexes of the list using "for loop"</p>
+        <p>because we know from the question that two of the lists have equal lengths</p>
+    </li>
+    <li>
+        <p>we get the value of high temperature and low temperature by getting through list notation</p>
+        <p>eg. high_temp[0],...</p>
+    </li>
+    <li>
+        <p>then we add the values of two temperature(high and low) and find the average temp</p>
+    </li>
+    <li>
+        <p>add them to the list by using <strong>append</strong></p>
+    </li>
+    <li>
+        <p>return the new_list</p>
+    </li>
+</ul>
+</div>

+ 25 - 0
loops2/explanations/explanation6.html

@@ -0,0 +1,25 @@
+<div>
+<p><strong>Explanation</strong></p>
+<p>1. Think about what is given.<p>
+<p>Given: Two strings </p>
+<p>2. What to find: number of times when both strings have same character at same index </p>
+<p>3. Steps needed:</p>
+<ul>
+    <li>
+        <p>initialize counter [to count the number of times]</p>
+    </li>
+    <li>
+        <p>iterate over the indexes of the list using "for loop"</p>
+        <p>because we know from the question that both of the strings have equal length</p>
+    </li>
+    <li>
+        <p>check whether the string 1 value is the same as string 2 value at the same index</p>
+    </li>
+    <li>
+        <p>if the above condition is true, we update the counter</p>
+    </li>
+    <li>
+        <p>return counter</p>
+    </li>
+</ul>
+</div>

+ 5 - 0
loops2/hints/hint0.html

@@ -0,0 +1,5 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Think how to flip 0 to 1 and 1 to 0 in just single code</p>
+<p>loop over the list containing 0 and 1</p>
+</div>

+ 10 - 0
loops2/hints/hint1.html

@@ -0,0 +1,10 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>What do you need to go over the list?</p>
+<p>
+    How will you iterate over multiple lists in a big list?
+</p>
+<p>
+    Think about how to compare the value(given) with those in lists.
+</p>
+</div>

+ 5 - 0
loops2/hints/hint2.html

@@ -0,0 +1,5 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Think about using nested loops [Why?]</p>
+<p>How will you add values of two coins and check with the given amount?</p>
+</div>

+ 6 - 0
loops2/hints/hint3.html

@@ -0,0 +1,6 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Think about looping over the indexes of the string</p>
+<p>How will you use the index to access the values from the list? </p>
+<p>Also, remember the right way to access an element from the list. (e.g. time[0] is the first element in the list)</p>
+</div>

+ 6 - 0
loops2/hints/hint4.html

@@ -0,0 +1,6 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Think about how to check whether there are extra spaces or not</p>
+<p>Clue: check at index (i-1) and i</p>
+<p>loop over the string</p>
+</div>

+ 7 - 0
loops2/hints/hint5.html

@@ -0,0 +1,7 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Iterate over the index of elements in both of the lists</p>
+<p>We know that the lengths of two lists are equal [given]</p>
+<p>How do you find the average?</p>
+<p>How do you add the elements to the list?</p>
+</div>

+ 7 - 0
loops2/hints/hint6.html

@@ -0,0 +1,7 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>We know that both of the strings have equal lengths [given]</p>
+<p>Think about how to loop over the indexes</p>
+<p>Think about how to access the values in list</p>
+<p>How will you get the number of times when both strings have the same character at the same index?</p>
+</div>

+ 5 - 0
loops2/hints/hint7.html

@@ -0,0 +1,5 @@
+<div>
+<p><strong>Hint</strong></p>
+<p>Need to know what <strong>palindrome</strong> is</p>
+<p>return boolean(True or False) to indicate whether the string is palindrome or not</p>
+</div>

+ 74 - 0
loops2/index.html

@@ -0,0 +1,74 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"/>
+<html>
+<head>
+<title>Lab 3: Loops II</title>
+<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
+<link rel="stylesheet" type="text/css" href="../css/style.css" />
+<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
+<script src="js/main.js"></script>
+</head>
+<body>
+<div id="container">
+
+<div id="header"><br><a href="../index.html"><button class="home" style="font-size:20px"><i class="fa fa-home" style="font-size:36px"></i>Home</button></a>
+	<h1><a href="https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/loops/LoopExercises2.pdf">Lab 3: Loops II</a></h1>
+</div>
+  <div id="wrapper">
+    <div id="content">
+	  <iframe id="contentframe" height="455px" width="100%" src="loops_intro.html" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>  
+	  <div id="buttons" style="display: none;">
+		  <button onclick="show_task()">Solve</button>
+		  <button onclick="show_hint()">Hint</button>
+		  <button onclick="show_solution()">Solution</button>
+		  <button onclick="show_explanation()">Explanation</button>
+		  <!--<button id="completed" onclick="mark_completed()">Do not show again</button>-->
+	  </div>
+    </div>
+  </div>
+  <div id="navigation"> 
+  <a href="https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/loops/LoopExercises2.pdf">Loops II</a>
+			<ol>
+			<!--<li><strong>Functions with numbers</strong>-->
+				<ul>
+					<li>
+						<a href="javascript:load_problem(0)">Flip challenge</a>
+					</li>
+					<li>
+						<a href="javascript:load_problem(1)">Nested contains</a>
+					</li>			
+					<li>
+						<a href="javascript:load_problem(2)">Can pay with 2 coins?</a>
+					</li>
+				</ul>
+
+				<!--<li><strong>Functions with strings</strong>-->
+				<ul>
+					<li>
+						<a href="javascript:load_problem(3)">Stretch string</a>
+					</li>
+					<li>
+						<a href="javascript:load_problem(4)">Remove extra spaces</a>
+					</li>
+					<li>
+						<a href="javascript:load_problem(5)">Avg daily temperatures</a>
+					</li>
+				</ul>
+				
+				
+				<!--<li><strong>Functions with IFs</strong>-->
+				<ul>
+					<li>
+						<a href="javascript:load_problem(6)">Count matches</a>
+					</li>
+					<li>
+						<a href="javascript:load_problem(7)">Palindromes</a>
+					</li>
+				</ul>
+			</ol>		
+  </div>  
+  <div id="footer">
+   <p>Virtual lab group, 2019</p>
+  </div>
+</div>
+</body>
+</html>

+ 109 - 0
loops2/js/main.js

@@ -0,0 +1,109 @@
+var data = [
+{problem:"https://repl.it/@AungWaiYanHein/Flip-challenge?lite=true", 
+solution:"solutions/solution0.py",
+hint:"hints/hint0.html",
+explanation:"explanations/explanation0.html"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Nested-contains?lite=true", 
+solution:"solutions/solution1.py",
+hint:"hints/hint1.html",
+explanation:"explanations/explanation1.html"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Can-pay-with-2-coins?lite=true", 
+solution:"solutions/solution2.py",
+hint:"hints/hint2.html",
+explanation:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/loops/can_pay_with2coins.mp4"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Stretch-string?lite=true", 
+solution:"solutions/solution3.py",
+hint:"hints/hint3.html",
+explanation:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/loops/stretch_strings.mp4"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Remove-extra-spaces?lite=true", 
+solution:"solutions/solution4.py",
+hint:"hints/hint4.html",
+explanation:"explanations/explanation4.html"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Average-daily-temperatures?lite=true", 
+solution:"solutions/solution5.py",
+hint:"hints/hint5.html",
+explanation:"explanations/explanation5.html"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Count-matches?lite=true", 
+solution:"solutions/solution6.py",
+hint:"hints/hint6.html",
+explanation:"explanations/explanation6.html"},
+
+{problem:"https://repl.it/@AungWaiYanHein/Palindromes?lite=true", 
+solution:"solutions/solution7.py",
+hint:"hints/hint7.html",
+explanation:"https://hope.simons-rock.edu/~mbarsky/intro18/tutorials/loops/PalindromeLecture2.mp4"}
+
+];
+
+var current_problem = -1;
+var current_included = 1;
+
+var problems = new Array(data.length);
+
+
+for (var i=0; i<problems.length; i++){
+	problems[i] = 1;
+}
+
+function load_problem(id){
+	current_problem = id;
+	current_included = problems[current_problem];
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].problem;
+	show_buttons();
+}
+
+
+function show_buttons(){
+	var buttons_elem = document.getElementById("buttons");
+	if (current_problem >=0){
+		buttons_elem.style.display = "inline-block";
+	}
+	else {
+		buttons_elem.style.display = "none";
+	}
+}
+
+function show_task(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].problem;
+	show_buttons();
+}
+
+function show_hint(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].hint;
+	show_buttons();
+}
+
+function show_solution(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].solution;
+	show_buttons();
+}
+
+function show_explanation(){
+	var iframe_elem = document.getElementById("contentframe");
+	iframe_elem.src = data[current_problem].explanation;
+	show_buttons();
+}
+
+function mark_completed(){
+	var include_elem = document.getElementById("completed");
+	if (current_included == 1){
+		include_elem.innerHTML = "Show again";
+		current_included = 0;
+		problems[current_problem] = 0;
+	}
+	else{
+		include_elem.innerHTML = "Do not show again";
+		current_included = 1;
+		problems[current_problem] = 1;
+	}
+}

+ 4 - 0
loops2/loops_intro.html

@@ -0,0 +1,4 @@
+<div>
+<p>In this lab you will exercise writing loops.</p>
+<p>Before you begin, review material about loops</p>
+</div>

+ 3 - 0
loops2/self-exercises.html

@@ -0,0 +1,3 @@
+<div>
+<p>This problem does not have a video explanation yet.</p>
+</div>

+ 21 - 0
loops2/solutions/solution0.py

@@ -0,0 +1,21 @@
+def flip_challenge(lst):
+  """
+    (list of int) -> list of int
+
+    return list of flipped values(0 or 1)
+
+    >>> flip_challenge([0,1,1,1,0,1,0,0,0])
+    [1,0,0,0,1,0,1,1,1]
+    >>> flip_challenge([0,1,0,0])
+    [1,0,1,1]
+  """
+
+  # initializing a new list
+  new_lst = []
+
+  # looping over the list
+  for num in lst:
+    # adding the flipped values to the new list
+    new_lst.append(1-num)
+  
+  return new_lst

+ 14 - 0
loops2/solutions/solution1.py

@@ -0,0 +1,14 @@
+def contains(value, lst):
+    """ (object, list of list of object) -> bool
+    Return whether value is an element of one of the nested lists in lst.
+    >>> contains('moogah', [[70, 'blue'], [1.24, 90, 'moogah'], [80, 100]])
+    True
+    """
+    found = False # We have not yet found value in the list.
+
+    for small_list in lst:
+      for element in small_list :
+        if value == element:
+          found = True
+          
+    return found

+ 21 - 0
loops2/solutions/solution2.py

@@ -0,0 +1,21 @@
+def can_pay_with_two_coins(denoms, amount):
+    """ (list of int, int) -> bool
+    Return True if and only if it is possible
+    to form amount, which is a number of cents,
+    using exactly two coins,
+    which can be of any of the
+    denominations in denoms.
+    >>> can_pay_with_two_coins([1, 5, 10, 25], 35)
+    True
+    >>> can_pay_with_two_coins([1, 5, 10, 25], 20)
+    True
+    >>> can_pay_with_two_coins([1, 5, 10, 25], 12)
+    #TODO: complete the example output
+    False
+    """
+    for coin1 in denoms:
+      for coin2 in denoms:
+        if coin1 + coin2 == amount:
+          return True
+    
+    return False

+ 18 - 0
loops2/solutions/solution3.py

@@ -0,0 +1,18 @@
+def stretch_string(s, stretch_factors):
+    """ (str, list of int) -> str
+    Precondition: len(s) == len(stretch_factors)
+    and the items of stretch_factors are non-negative integers
+
+    Return a string consisting of the characters in s
+    in the same order as in s, repeated the number of times
+    indicated by the item
+    at the corresponding position of stretch_factors.
+    >>> stretch_string('Hello', [2, 0, 3, 1, 1])
+    'HHllllo'
+    """
+    new_string = ""
+
+    for i in range(len(s)):
+      new_string += s[i] * stretch_factors[i]
+
+    return new_string

+ 30 - 0
loops2/solutions/solution4.py

@@ -0,0 +1,30 @@
+def remove_extra_spaces(s):
+  """
+    (str) -> str
+
+    returns string that is the same as s but with each
+    group of multiple spaces replaced by a single space
+
+    >>> remove_extra_spaces('Hello     World')
+    'Hello World'
+    >>> remove_extra_spaces('remove     extra    spaces')
+    'remove extra spaces'
+  """
+  # intialize two variables: one to store the previous char and another one to store the new string
+  last_char = ""
+  new_string = ""
+
+  # loop over the characters in the string
+  for char in s:
+    # check if the last character and this character have spaces
+    if last_char == " " and char == " ":
+      #remove extra spaces
+      char = ""
+    else:
+      # update the last_char to char
+      last_char = char    
+
+      # update new_string by concatenation
+      new_string += char
+
+  return new_string  

+ 24 - 0
loops2/solutions/solution5.py

@@ -0,0 +1,24 @@
+def average_daily_temp(high_temps, low_temps):
+    """
+    (list of number, list of number) -> list of float
+    Precondition: len(high_temps) == len(low_temps)
+    high_temps and low_temps are daily high and low temperatures
+    for a series of days.
+    Return a new list of temperatures where
+    each item is the daily average.
+    >>> average_daily_temp ([26, 27, 27, 26], [20, 20, 21, 21])
+    [23.0, 23.5, 24.0, 23.5]
+    """
+    new_list = []
+    for index in range(len(high_temps)):
+
+      #adding two values eg. 26 + 20
+      temp_sum = high_temps[index] + low_temps[index]
+
+      #avg = (num1+num2+...+numN)/number of elements
+      avg_daily_temp = temp_sum/2.0           
+
+      #adding to the new_list          
+      new_list.append(avg_daily_temp)                   
+    
+    return new_list

+ 23 - 0
loops2/solutions/solution6.py

@@ -0,0 +1,23 @@
+def count_matches(s1, s2):
+    """ (str, str) -> int
+    Precondition: len(s1) == len(s2)
+    Return the number of positions in s1
+    that contain the same character at the
+    corresponding position of s2.  
+
+    >>> count_matches("hello","itchy")
+    0
+    >>> count_matches("weather","charter")
+    3
+    """
+    #initializing counter
+    same_char_counter = 0
+
+    for char_index in range(len(s1)):
+      # check if the value at that specific index in
+      # both string are equal
+      if s1[char_index] == s2[char_index]:
+        #update the counter
+        same_char_counter += 1
+    
+    return same_char_counter

+ 26 - 0
loops2/solutions/solution7.py

@@ -0,0 +1,26 @@
+def is_palindrome(s):
+  """
+    (str) -> bool
+
+    Return True if and only if s is a palindrome.
+
+    >>> is_palindrome('noon')
+    True
+    >>> is_palindrome('racecar')
+    True
+    >>> is_palindrome('dented')
+    False
+  """
+  # initialize new string to store words in a reverse order
+  reversed_word = ""
+
+  # loop over the original word
+  for character in s:
+    # store word in reverse order
+    reversed_word = character + reversed_word
+  
+  # check whether the reversed_word is equal to original word
+  if reversed_word == s:
+    return True
+  
+  return False