Lab9 (1) - Jupyter Notebook
.pdf
keyboard_arrow_up
School
Texas Tech University *
*We aren’t endorsed by this school
Course
1330
Subject
Computer Science
Date
Dec 6, 2023
Type
Pages
16
Uploaded by juanchozulu31
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
1/16
Laboratory 9: Count Controlled Repetition
In [6]:
Full name: Juan Zuluaga
R#: 11830028
Title of the notebook: Loops, Looops, Loooooops
Date: 09/28/23
Juans-MacBook-Pro.local
juanandreszuluqga
/Users/juanandreszuluqga/anaconda3/bin/python
3.11.4 (main, Jul
5 2023, 09:00:44) [Clang 14.0.6 ]
sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial
=0)
# Preamble script block to identify host, user, and kernel
import
sys
!
hostname
!
whoami
print
(sys.executable)
print
(sys.version)
print
(sys.version_info)
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
2/16
Program flow control (Loops)
Controlled repetition
Structured FOR Loop
Structured WHILE Loop
Count controlled repetition
Count-controlled repetition is also called definite repetition because the number of repetitions is
known before the loop begins executing. When we do not know in advance the number of times
we want to execute a statement, we cannot use count-controlled repetition. In such an instance,
we would use sentinel-controlled repetition.
A count-controlled repetition will exit after running a certain number of times. The count is kept in a
variable called an index or counter. When the index reaches a certain value (the loop bound) the
loop will end.
Count-controlled repetition requires
control variable (or loop counter)
initial value of the control variable
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
3/16
increment (or decrement) by which the control variable is modified each iteration through the
loop
condition that tests for the final value of the control variable
We can use both
for
and
while
loops, for count controlled repetition, but the
for
loop in
combination with the
range()
function is more common.
Structured
FOR
loop
We have seen the for loop already, but we will formally introduce it here. The
for
loop executes a
block of code repeatedly until the condition in the
for
statement is no longer true.
Looping through an iterable
An iterable is anything that can be looped over - typically a list, string, or tuple. The syntax for
looping through an iterable is illustrated by an example.
First a generic syntax
for a in iterable:
print(a)
Notice the colon
:
and the indentation. Now a specific example:
Example: A Loop to Begin With!
Make a list with "Walter", "Jesse", "Gus, "Hank". Then, write a loop that prints all the elements of
your lisk.
In [7]:
The
range()
function to create an iterable
The
range(begin,end,increment)
function will create an iterable starting at a value of begin,
in steps defined by increment (
begin += increment
), ending at
end
.
So a generic syntax becomes
Walter
Jesse
Gus
Hank
# set a list
BB
=
[
"Walter"
,
"Jesse"
,
"Gus"
,
"Hank"
]
# loop thru the list
for
AllStrings
in
BB:
print
(AllStrings)
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
4/16
for a in range(begin,end,increment):
print(a)
In [8]:
Example: That's odd!
Write a loop to print all the odd numbers between 0 and 10.
In [9]:
Walter
Jesse
Gus
Hank
1
3
5
7
9
# set a list
BB
=
[
"Walter"
,
"Jesse"
,
"Gus"
,
"Hank"
]
# loop thru the list
for
i
in
range
(
0
,
4
,
1
):
# Change the numbers, what happens?
print
(BB[i])
# For loop with range
for
x
in
range
(
1
,
10
,
2
):
# a sequence from 2 to 5 with steps of 1
print
(x)
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
5/16
Nested Repetition | Loops within Loops
Round like a circle in a spiral, like a wheel within a wheel
Never ending or beginning on an ever spinning reel
Like a snowball down a mountain, or a carnival balloon
Like a carousel that's turning running rings around the moon
Like a clock whose hands are sweeping past the minutes of its face
And the world is like an apple whirling silently in space
Like the circles that you find in the windmills of your mind!
Windmills of Your Mind lyrics © Sony/ATV Music Publishing LLC, BMG Rights
Management
Songwriters: Marilyn Bergman / Michel Legrand / Alan Bergman
Recommended versions: Neil Diamond | Dusty Springfield | Farhad Mehrad
"Like the circles that you find in the windmills of your mind", Nested repetition is when a control
structure is placed inside of the body or main part of another control structure.
break
to exit out of a loop
Sometimes you may want to exit the loop when a certain condition different from the counting
condition is met. Perhaps you are looping through a list and want to exit when you find the first
element in the list that matches some criterion. The break keyword is useful for such an operation.
For example run the following program:
In [10]:
i =
0 j =
2
i =
1 j =
4
i =
2 j =
6
#
j
=
0
for
i
in
range
(
0
,
5
,
1
):
j
+=
2
print
(
"i = "
,i,
"j = "
,j)
if
j
==
6
:
break
9/29/23, 12:02 PM
Lab9 (1) - Jupyter Notebook
localhost:8888/notebooks/Lab9 (1).ipynb#
6/16
In [11]:
In the first case, the for loop only executes 3 times before the condition j == 6 is TRUE and the loop
is exited. In the second case, j == 7 never happens so the loop completes all its anticipated
traverses.
In both cases an
if
statement was used within a for loop. Such "mixed" control structures are
quite common (and pretty necessary). A
while
loop contained within a
for
loop, with several
if
statements would be very common and such a structure is called
nested control.
There is
typically an upper limit to nesting but the limit is pretty large - easily in the hundreds. It depends on
the language and the system architecture ; suffice to say it is not a practical limit except possibly
for general-domain AI applications.
We can also do mundane activities and leverage loops, arithmetic, and format codes to make
useful tables like
Example: Cosines in the loop!
Write a loop to print a table of the cosines of numbers between 0 and 0.01 with steps of 0.001.
i =
0 j =
2
i =
1 j =
4
i =
2 j =
6
i =
3 j =
8
i =
4 j =
10
# One Small Change
j
=
0
for
i
in
range
(
0
,
5
,
1
):
j
+=
2
print
(
"i = "
,i,
"j = "
,j)
if
j
==
7
:
break
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
O RottenAnchoredDecagons x
M (no subject) - jcofield122e x
O Lab 6: Module 6 - Repetitis x
https://tcc.instructure.com/courses/42392/assignments/976548?module_item_id32464159
-Write a program with af
as Program for Fibonacci nu x i1390rmc01171ipg (1920 x0 1390
Write a program with a for loop that displays the numbers 1 to 10 and their respective cubes. You may want to use the range function. Your output should look like the example
Number
Cube
******
1
8
27
4
64
125
5.
216
343
512
8.
729
9.
1000
10
To format your table you can use the tab syntax, \t, to tab your output. For example:
print("Number\t Square")
arrow_forward
Write in assembly language LC3
LC3 simulator to use: https://wchargin.com/lc3web/
Write a program that implements a while loopa. Loop while(R0 > -10)b. For every iteration of the loop subtract 2 from R0c. R0 should start at 0
Use LC3 simulator : https://wchargin.com/lc3web/
Psuedo code:
.ORIG x3000
; NOTE: it says to use R0 for the result; instead, use R3 so the result does not get overwritten when HALT command executes.
; Initialize R3 with #0
; Set R1 to #10
LOOP ; Decrease R3 by #2
; Add R1 and R3 and store into R2
; Branch if positive back to top of loop (we have reached our end condition)
DONE ; HALT CPU
.END
arrow_forward
Computer Science
Please answer this question in assembly language with .asm extension.
The code given in 99Heater.asm file is:
; ===== Heater and Thermostst on Port 03 ==========================; ===== 99Heater.asm ==============================================; ===== Heater and Thermostst on Port 03 ========================== MOV AL,0 ; Code to turn the heater off OUT 03 ; Send code to the heater
IN 03 ; Input from Port 03 AND AL,1 ; Mask off left seven bits JZ Cold ; If the result is zero, turn the heater on HALT ; Quit
Cold: MOV AL,80 ; Code to turn the heater on OUT 03 ; Send code to the heater END; =================================================================
Fix the program 99Heater.asm so that the temperature will stay at 21 ºC.
Please solve the question in assembly language. I will definitely give you THUMBS UP.
arrow_forward
Which of the following best describes SVML?
SVML is a banner marketing term that encompasses all Intel SIMD features
SVML is a library that uses SIMD operations to provide support for operations
that don't map to single SIMD intrinsics
SVML is an instruction set extension to provide support for cryptographic
operations
SVML is the 512-bit version of AVX
SVML is the third version of AVX
arrow_forward
OS ASSEMBLY CODE
Using YASM assembly code(x86)
.Create a program that accepts two user input. For any input from the two users, return True or false
Example:
Is today Friday ?
UserA: True
UserB: False
UserA is Wrong
UserB is Correct
arrow_forward
dont post existing answer else direct dislike
arrow_forward
PYTHON/JUPYTER NOTEBOOKS
given the attached fourier data :
# Measurements of fourier data data = [ [-2.00, -9.37], [-1.00, 10.00], [-1.14, 10.83], [-0.29, -13.88], [0.00, -18.00], [0.57, -1.83], [1.00, 14.00], [1.43, 14.5], [2.00, 0.00], [2.29, -1.38], [3.00, 28.00], [3.14, 35.64], [4.00, 37.88], [5.00, -23.00], [4.86, -17.52], [5.71, -14.63], [6.00, -1.00], [6.50, -15.00], [6.57, -1.73], [7.00, 12.00], [7.43, 5.97], [7.50, 2.00], [8.29, 22.78], [9.00, 1.00], [9.14, -6.41], [10.0, -9.37]
Fit the data with the linear least-squares fit method using Fourier basis functions. Use 15 pairs of sines and cosines and create the plots/results below.
x_vec = [ 1.591 -0.42 5.684 -5.112 -2.257 0.36 4.546 -7.626 0.341 -4.391 1.092 1.878 4.286 7.783 -3.427 -4.608 1.763 -0.957 -1.751 3.441 2.857 -2.624 2.96 0.911 -2.538 2.782 -1.943 -8.819 1.635 2.123 2.123]
Local Relative Error (discarding y values < 0.1):
mean [%] = 17.08
std [%] = 89.43…
arrow_forward
//----------------------------------------------------------------------------// LLNode.java by Dale/Joyce/Weems Chapter 3//// Implements <T> nodes for a Linked List.//----------------------------------------------------------------------------
package Linkedqueue;
public class LLNode<T>{private LLNode<T> link;private T info;public LLNode(T info){this.info = info;link = null;}public void setInfo(T info)// Sets info of this LLNode.{this.info = info;}
public T getInfo()// Returns info of this LLONode.{return info;}public void setLink(LLNode<T> link)// Sets link of this LLNode.{this.link = link;}
public LLNode<T> getLink()// Returns link of this LLNode.{return link;}}
//----------------------------------------------------------------------------// LLNode.java by Dale/Joyce/Weems Chapter 3//// Implements <T> nodes for a Linked List.//----------------------------------------------------------------------------
package Linkededstack;
public class…
arrow_forward
Connect the following components to the Raspberry Pi using a breadboard.
1. LED: PIN number 30
2. Push Button: PIN numbe 38
3. Write the code in python 3 to read the push button and control the LED such that :
a) When the button is pressed, the LED turns ON
b) When the button is released, the LED blinks with a delay of 500ms.
arrow_forward
using emulator8086 provide the working code follow the instruction below and kindly screenshot thankyou
THE POSSIBLE OUTPUT AND INPUT IS ALREADY PROVIDED
arrow_forward
Answser must be in MIPSzy assembly language.
Max of 3 - Branch
arrow_forward
SPEC ratios are shown for the Pentium and the Pentium Pro (Pentium+) processors
Clock Pentium Pentium+ Pentium Pentium+
Rate SPECint SPECint SPECfp SPECfp
100 MHz 3.2
150 MHZ
4.3
200 MHZ 5.5
N/A
6.0
8.0
2.6 N/A
3.0
5.1
3.8
6.8
What can we learn from this information?
For example,
1). SPECint shows Pentium+ is 1.4 to 1.45 times faster than Pentium. How about SPECfp results?
2). Clock rate of the Pentium doubles from 100 MHz to 200 MHz, are both the SPECint and SPECfp performance
doubled correspondingly?
arrow_forward
Create a loop using the pattern via Python
arrow_forward
Write a Python code to be used with a Raspberry Pi to control the angle of a servo motor
using pushbuttons and a switch, your code should satisfy the following table:
Truth Table
Switch
Pushbutton 1
Pushbutton 2
Servo's angle
1
No Change
1
Rotate slowly towards 180 degrees
1
1
Rotate slowly towards 0 degree
1
1
1
No Change
Angle is 0 degree
Angle is 0 degree
1
Angle is 0 degree
1
Angle is 0 degree
Submit a schematic for the needed connections (You can use a software or you can sketch
clearly by hand)
arrow_forward
What is meant by Kernel in Linux system
arrow_forward
Using Java
arrow_forward
Computer Science
MSP432 Code Composer Studio using ARM-Cortex M4 Assembly. Make a linear congressional generator (Random function generator) as a subroutine, so whenever switch 1 is called (port 6 Bit 0 or P6.0) it outputs a random LED turning on from 6 external LEDS. I just need the "random" Function so I can call it to the subroutine that compares if switch 1 is being pressed
arrow_forward
Are Ubuntu Linux distributions based on a particular Linux distribution?
arrow_forward
ssembly language
arrow_forward
Please use Scala to solve this question
package gatetasks
import tinylog._
object factory:
/**
Returns a gate containing the eXclusive OR of the two input gates
*/
def buildXOR(in1 : Gate, in2: Gate) : Gate =
// TASK 1: Return a gate which contains the exclusive or (XOR) of the two
// input gates.
// Hints: - Remember that gates can be combined to create new gates using
// logic operators || and && and negated using !
// - You can NOT check/compare the .value method of the gates as
// values are not set at the time you 'connect' the gates
// (remember that you are building a [very small] circuit)
// Write your formula here:
???
end buildXOR
/**
* Returns the majority state of gates in1, in2, in3
*/
def buildMajorityOfThree(in1 : Gate, in2 : Gate, in3 : Gate) : Gate =
// TASK 2: Return a gate which evaluates to
// false - if two or three are false
//…
arrow_forward
What are loops in Datawarehousing?,
arrow_forward
Considering Dot matrix display write an assembly language code such that, first row of dot matrix should glow after that row 2 and same will follow till last row; repeat this pattern (last digit of your student ID for example: 40036 “6 times”) times in a
arrow_forward
Is there a version of Windows 7 that just supports 32-bit computing, and is it available for purchase?
arrow_forward
implement a Python script that
checks whether the file "tmp" exists (hint: take a look at the "os" module of Python)
checks whether the file "tmp" is readable
checks whether the file "tmp" is writable
checks whether the file "tmp" is executable
arrow_forward
import org.firmata4j.Pin;import org.firmata4j.ssd1306.SSD1306;import java.util.TimerTask;class task extends TimerTask {private final Pin carbonSensor;private final SSD1306 oledScreen;private boolean lastCarbonState = false; // Track previous carbon statetask(Pin sensor, SSD1306 screen) {this.carbonSensor = sensor;this.oledScreen = screen;}@Overridepublic void run() {int sensorValue = (int) carbonSensor.getValue(); // Get the sensor valueboolean currentCarbonState = sensorValue < 5000; // Change to less than 300System.out.println("Sensor Value: " + sensorValue); // Print sensor valueif (currentCarbonState != lastCarbonState) {oledScreen.getCanvas().clear();if (sensorValue<500) {try {oledScreen.getCanvas().drawString(0, 15, "THERE IS A FIRE DETECTED");oledScreen.getCanvas().drawString(0, 26, "YOU MUST EVACUATE BUILDING");System.out.println("Individual in danger");} catch (Exception e) {throw new RuntimeException(e);}} if (sensorValue>500) {oledScreen.getCanvas().drawString(0,…
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:9781337569798
Author:ECKERT
Publisher:CENGAGE L
Related Questions
- O RottenAnchoredDecagons x M (no subject) - jcofield122e x O Lab 6: Module 6 - Repetitis x https://tcc.instructure.com/courses/42392/assignments/976548?module_item_id32464159 -Write a program with af as Program for Fibonacci nu x i1390rmc01171ipg (1920 x0 1390 Write a program with a for loop that displays the numbers 1 to 10 and their respective cubes. You may want to use the range function. Your output should look like the example Number Cube ****** 1 8 27 4 64 125 5. 216 343 512 8. 729 9. 1000 10 To format your table you can use the tab syntax, \t, to tab your output. For example: print("Number\t Square")arrow_forwardWrite in assembly language LC3 LC3 simulator to use: https://wchargin.com/lc3web/ Write a program that implements a while loopa. Loop while(R0 > -10)b. For every iteration of the loop subtract 2 from R0c. R0 should start at 0 Use LC3 simulator : https://wchargin.com/lc3web/ Psuedo code: .ORIG x3000 ; NOTE: it says to use R0 for the result; instead, use R3 so the result does not get overwritten when HALT command executes. ; Initialize R3 with #0 ; Set R1 to #10 LOOP ; Decrease R3 by #2 ; Add R1 and R3 and store into R2 ; Branch if positive back to top of loop (we have reached our end condition) DONE ; HALT CPU .ENDarrow_forwardComputer Science Please answer this question in assembly language with .asm extension. The code given in 99Heater.asm file is: ; ===== Heater and Thermostst on Port 03 ==========================; ===== 99Heater.asm ==============================================; ===== Heater and Thermostst on Port 03 ========================== MOV AL,0 ; Code to turn the heater off OUT 03 ; Send code to the heater IN 03 ; Input from Port 03 AND AL,1 ; Mask off left seven bits JZ Cold ; If the result is zero, turn the heater on HALT ; Quit Cold: MOV AL,80 ; Code to turn the heater on OUT 03 ; Send code to the heater END; ================================================================= Fix the program 99Heater.asm so that the temperature will stay at 21 ºC. Please solve the question in assembly language. I will definitely give you THUMBS UP.arrow_forward
- Which of the following best describes SVML? SVML is a banner marketing term that encompasses all Intel SIMD features SVML is a library that uses SIMD operations to provide support for operations that don't map to single SIMD intrinsics SVML is an instruction set extension to provide support for cryptographic operations SVML is the 512-bit version of AVX SVML is the third version of AVXarrow_forwardOS ASSEMBLY CODE Using YASM assembly code(x86) .Create a program that accepts two user input. For any input from the two users, return True or false Example: Is today Friday ? UserA: True UserB: False UserA is Wrong UserB is Correctarrow_forwarddont post existing answer else direct dislikearrow_forward
- PYTHON/JUPYTER NOTEBOOKS given the attached fourier data : # Measurements of fourier data data = [ [-2.00, -9.37], [-1.00, 10.00], [-1.14, 10.83], [-0.29, -13.88], [0.00, -18.00], [0.57, -1.83], [1.00, 14.00], [1.43, 14.5], [2.00, 0.00], [2.29, -1.38], [3.00, 28.00], [3.14, 35.64], [4.00, 37.88], [5.00, -23.00], [4.86, -17.52], [5.71, -14.63], [6.00, -1.00], [6.50, -15.00], [6.57, -1.73], [7.00, 12.00], [7.43, 5.97], [7.50, 2.00], [8.29, 22.78], [9.00, 1.00], [9.14, -6.41], [10.0, -9.37] Fit the data with the linear least-squares fit method using Fourier basis functions. Use 15 pairs of sines and cosines and create the plots/results below. x_vec = [ 1.591 -0.42 5.684 -5.112 -2.257 0.36 4.546 -7.626 0.341 -4.391 1.092 1.878 4.286 7.783 -3.427 -4.608 1.763 -0.957 -1.751 3.441 2.857 -2.624 2.96 0.911 -2.538 2.782 -1.943 -8.819 1.635 2.123 2.123] Local Relative Error (discarding y values < 0.1): mean [%] = 17.08 std [%] = 89.43…arrow_forward//----------------------------------------------------------------------------// LLNode.java by Dale/Joyce/Weems Chapter 3//// Implements <T> nodes for a Linked List.//---------------------------------------------------------------------------- package Linkedqueue; public class LLNode<T>{private LLNode<T> link;private T info;public LLNode(T info){this.info = info;link = null;}public void setInfo(T info)// Sets info of this LLNode.{this.info = info;} public T getInfo()// Returns info of this LLONode.{return info;}public void setLink(LLNode<T> link)// Sets link of this LLNode.{this.link = link;} public LLNode<T> getLink()// Returns link of this LLNode.{return link;}} //----------------------------------------------------------------------------// LLNode.java by Dale/Joyce/Weems Chapter 3//// Implements <T> nodes for a Linked List.//---------------------------------------------------------------------------- package Linkededstack; public class…arrow_forwardConnect the following components to the Raspberry Pi using a breadboard. 1. LED: PIN number 30 2. Push Button: PIN numbe 38 3. Write the code in python 3 to read the push button and control the LED such that : a) When the button is pressed, the LED turns ON b) When the button is released, the LED blinks with a delay of 500ms.arrow_forward
- using emulator8086 provide the working code follow the instruction below and kindly screenshot thankyou THE POSSIBLE OUTPUT AND INPUT IS ALREADY PROVIDEDarrow_forwardAnswser must be in MIPSzy assembly language. Max of 3 - Brancharrow_forwardSPEC ratios are shown for the Pentium and the Pentium Pro (Pentium+) processors Clock Pentium Pentium+ Pentium Pentium+ Rate SPECint SPECint SPECfp SPECfp 100 MHz 3.2 150 MHZ 4.3 200 MHZ 5.5 N/A 6.0 8.0 2.6 N/A 3.0 5.1 3.8 6.8 What can we learn from this information? For example, 1). SPECint shows Pentium+ is 1.4 to 1.45 times faster than Pentium. How about SPECfp results? 2). Clock rate of the Pentium doubles from 100 MHz to 200 MHz, are both the SPECint and SPECfp performance doubled correspondingly?arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.Computer ScienceISBN:9781337569798Author:ECKERTPublisher:CENGAGE L
LINUX+ AND LPIC-1 GDE.TO LINUX CERTIF.
Computer Science
ISBN:9781337569798
Author:ECKERT
Publisher:CENGAGE L