Solutions for the second quiz COSC 1306 Fall 2104.

Slides:



Advertisements
Similar presentations
Computer Programming Mr. José A. Ortiz Morris. Computer Language  Languages that the computer understands.  They are low level languages. (BINARY 1.
Advertisements

Type Title Here for Tic-Tac-Toe Type names of students in group here.
Programming Types of Testing.
LBSC 690 Session #10 Programming, JavaScript Jimmy Lin The iSchool University of Maryland Wednesday, November 5, 2008 This work is licensed under a Creative.
Computer Science 1620 Programming & Problem Solving.
Chapter 2 Writing Simple Programs
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
The fourth programming assignment J.-F. Pâris Fall 2014.
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
6 Steps of the Programming Process
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
COSC 1306 COMPUTER LITERACY FOR SCIENCE MAJORS Jehan-François Pâris
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Higher Grade Computing Studies 2. Languages and Environments Higher Computing Software Development S. McCrossan 1 Classification of Languages 1. Procedural.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Variables and Expressions CMSC 201 Chang (rev )
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Lesson 6. Python 3.3 Objectives. In this lesson students will learn how to output data to the screen and request input from the user. Students will also.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CS 100 Introduction to Computing Seminar
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Files Tutor: You will need ….
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
The Hashemite University Computer Engineering Department
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
SOLUTIONS TO THE QUESTIONS DISCUSSED DURING THE FIRST REVIEW SESSION COSC 1306 Fall 2013.
Compilers and Interpreters
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
CHAPTER 2 GC101 Program’s algorithm 1. COMMUNICATING WITH A COMPUTER  Programming languages bridge the gap between human thought processes and computer.
Chapter 2 Writing Simple Programs
Agenda Introduction Computer Programs Python Variables Assignment
Lesson 08: Files Class Participation: Class Chat: Attendance Code 
Lecture 4: Expressions and Variables
Formatted Input/Output
Line Continuation, Output Formatting, and Decision Structures
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 2 Assignment and Interactive Input
Lecture 24: print revisited, tuples cont.
Chapter 4: Algorithm Design
Line Continuation, Output Formatting, and Decision Structures
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
Math and Data Types Practice Problems
An Introduction to Python
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
Remembering lists of values lists
Problem Solving Skill Area 305.1
Chapter 3: Selection Structures: Making Decisions
Lecture 4: Expressions and Variables
Introduction to Programming with Python
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Chapter 3: Selection Structures: Making Decisions
CISC101 Reminders Assignment 3 due today.
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
def-ining a function A function as an execution control structure
Hossain Shahriar CISC 101: Fall 2011 Hossain Shahriar
Presentation transcript:

Solutions for the second quiz COSC 1306 Fall 2104

First Question What is the difference between a compiled language and an interpreted language? (2×5 points)

Answer What is the difference between a compiled language and an interpreted language? (2×5 points)  A compiled language is translated before execution into binary code that can be directly executed.  An interpreted language is interpreted just before and during execution into something executable.

Second question The University of Houston stores your names in a “last name first followed by a comma and no space” format as in “Edison,Thomas Alva”. Write a Python 3 function adding a space after the comma to and return something like “Edison, Thomas Alva”, which is more pleasing to the eye. (2×5 points). (Hint: I would split the string at the comma and rebuild it with the comma and the space.)

Answer def student_name(uh_name): lst = uh_name.split(',') return lst[0] + ', ' + lst[1] Space

Second question (variant) The University of Houston stores your names in a “last name first followed by a comma and no space” format as in “Edison,Thomas Alva”. Write a Python 3 function adding a space after the comma to and return something like “Thomas Alva Edison,”, which is more pleasing to the eye. (2×5 points). (Hint: I would split the string at the comma and rebuild it with the space.)

Answer def student_name(uh_name) : lst = uh_name.split(',') return lst[1] + ' ' + lst[0]

Third question Write a Python function computing restaurant tips.  Your function should have as inputs the amount on the check the purchase and the tip rate in percent.  In addition, it should assume a default tip rate of 15 percent.  For instance, tip(50) should return 7.50 and tip(100, 18) should return 18. (2×5 points)

Answer def tip(bill, rate = 15) : return bill*rate/100

Fourth question Consider the following list containing two events, which themselves are lists:  sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']] What would be the outcomes of the following Python statements, taken individually? (3×5 points)  sched [0][0] = '1100'  sched [0].append('Must attend')  sched.pop(0)

Answer sched = [['1030', 'Faculty meeting'] ['1430', 'COSC 1306']]  sched [0][0] = '1100' [['1100', 'Faculty meeting'] ['1430', 'COSC 1306']]  sched [0].append('Must attend') [['1030', 'Faculty meeting', 'Must attend'] ['1430', 'COSC 1306']]  sched.pop(0) [['1430', 'COSC 1306']]

Fifth question If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points)  lst.pop()  lst[0:1]  lst[0]  lst[:]

Answer If lst = ['Ann', 'Barbara', 'Charles'], what would be the outcomes of the following Python statements, taken individually? (4×5 points)  lst.pop() ['Ann', 'Barbara']  lst[0:1] ['Ann']  lst[0] 'Ann'  lst[:] ['Ann', 'Barbara', 'Charles']

Sixth question Complete the following program to have it compute the sum of all numbers entered. Your program should end once the user has entered a single minus sign and then print the total. (4×5 points)

Sixth question astring =input('Enter a number or a minus to terminate: ') sum = ___ while ___________: sum =___________ ________________ print('Total is ' + str(sum))

Answer astring =input('Enter a number or a minus to terminate: ') sum = 0 while astring != '-' : sum =sum + float(astring) astring =input('Enter a number or a minus to terminate: ') print('Total is ' + str(sum))

Seventh question Professor Jenkins has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Avarez, Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (3×5 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)

Seventh question def semester_average(record): ___________________________ if ________ : return ______________ else: return ______________

Answer def semester_average(record): essays= record[1:] # eliminate first entry if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)

Seventh question (variant) Professor Patel has to compute her semester averages. Each of her student records consists of a student name followed by the grades she gave for the essays she assigned to her students as in: ['Alvarez', 'Alonzo', 85, 70, 90, 85] Given that all essays have the same weight, what code will she write? (3×5 points) (Hint: use the sum() and len() methods and handle correctly students who have not turned in any essay.)

Answer def semester_average(record): essays= record[2:] # eliminate two entries if len(essays) == 0 : return 0 else: return sum(essays)/len(essays)