14 views

Uploaded by Kraxus

wHAT TO DO

- 03_HW
- ch2_slides_2.pdf
- Java Phase 1 Object Oriented Programming Through Java
- ch2_slides.pdf
- Integers and RPG
- Twice the Bits, Twice the Trouble - Vulnerabilities Induced by Migrating to 64-Bit Platforms
- G52CPP_2012 Lecture 2 Final
- An Introduction to Java Programming for First-time Programmers - Java Programming Tutorial
- c-manual 14
- 02 Slide
- Cisco Bts 10200 Soft Switch
- MIS387-3
- Summary Primitive Types and Expressions
- spec_test_1_for_BB
- L02
- j2me lab
- 4.MD.4 Task 3
- Excel Text Function 2
- MELJUN CORTES Programming Languages Declarations
- Python_Programming

You are on page 1of 4

Cluj Napoca

Faculty of Mathematics and Computer Science

Fundamentals of Programming

CONTENTS

Python programs … ......................................................................................................................................................................................... 2

Basic Data Types (Int, Boolean) ............................................................................................................................................................ 2

Problems With Strings .............................................................................................................................................................................. 3

Problems with Lists .................................................................................................................................................................................... 4

Python basics – easy to understand, interpreted, dynamically typed, modern, fully-featured language

Write and run simple programs using console input/output and the list basic structured data type

Use procedural programming, making sure each function has a single responsibility

Babeș-Bolyai University

Cluj Napoca

Faculty of Mathematics and Computer Science

Fundamentals of Programming

PYTHON PROGRAMS …

(http://codingbat.com/prob/p124984)

'''

Returns True if one of the parameters or their sum is 10

a,b - integers

'''

return (a == 10 or b == 10 or a + b == 10)

b = int(input("Give second int: "))

print("Result is ", makes10(a, b))

2. The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on

vacation. We sleep in if it is not a weekday or we're on vacation. Return True if we sleep in.

(http://codingbat.com/prob/p173401)

'''

Checks whether we can sleep in

weekday - Boolean parameter for weekday

vacation - Boolean parameter for vacation

Return True if it is not a weekday or we are on vacation, False otherwise

'''

if not weekday or vacation:

return True

else:

return False

weekday = (weekday == 'yes')

vacation = (vacation == 'yes')

print("Do we sleep in? ", sleepIn(weekday, vacation))

Question – How do we validate input in the code above? What should happen if the user enters

something else than yes/no?

Babeș-Bolyai University

Cluj Napoca

Faculty of Mathematics and Computer Science

Fundamentals of Programming

stringSplosion('Code') → 'CCoCodCode'

stringSplosion('abc') → 'aababc'

stringSplosion('ab') → 'aab'

(http://codingbat.com/prob/p118366)

def stringSplosion(string):

'''

TODO - Write specification

'''

result = ""

for i in range(len(string)):

result = result + string[:i + 1]

return result

print(stringSplosion(string))

4. Given 2 strings, a and b, return the number of the positions where they contain the same length 2

substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the

same place in both strings.

stringMatch('xxcaazz', 'xxbaaz') → 3

stringMatch('abc', 'abc') → 2

stringMatch('abc', 'axc') → 0

(http://codingbat.com/prob/p182414)

'''

TODO - Write specification

'''

# Figure which string is shorter.

shorter = min(len(a), len(b))

count = 0

# Use length-1 here, so can use char str[i+1] in the loop

for i in range(shorter - 1):

a_sub = a[i:i + 2]

b_sub = b[i:i + 2]

if a_sub == b_sub:

count = count + 1

return count

stringB = input("Give second string:")

print("Number of positions that match: ", stringMatch(stringA, stringB))

Babeș-Bolyai University

Cluj Napoca

Faculty of Mathematics and Computer Science

Fundamentals of Programming

5. Return the number of even ints in the given list. Note: the % "mod" operator computes the remainder,

e.g. 5 % 2 is 1.

(http://codingbat.com/prob/p189616)

def readNumberList():

myList = []

while True:

x = int(input("Enter list item (-1 to finish):"))

if x == -1:

break

myList.append(x)

return myList

def evenCount(myList):

'''

TODO - Write specification

'''

result = 0

for n in myList:

if n % 2 == 0:

result += 1

return result

lst = readNumberList()

print("Number of even elements is ", evenCount(lst))

6. Return the sum of the numbers in a list, returning 0 for an empty list. Except the number 13 is very

unlucky, so it does not count and numbers that come immediately after a 13 also do not count.

(http://codingbat.com/prob/p167025)

def sumWithout13(lst):

'''

TODO - Write specification

'''

result = 0

if lst[0] != 13:

result = lst[0]

index = 1

if lst[index] != 13 and lst[index - 1] != 13:

result += lst[index]

index += 1

return result

lst = readNumberList()

print("Sum without 13s is ", sumWithout13(lst))

- 03_HWUploaded bySandipChowdhury
- ch2_slides_2.pdfUploaded byAnonymous 7CxwuBUJz3
- Java Phase 1 Object Oriented Programming Through JavaUploaded bypremaims
- ch2_slides.pdfUploaded byFatimaTrinidad
- Integers and RPGUploaded byrachmat99
- Twice the Bits, Twice the Trouble - Vulnerabilities Induced by Migrating to 64-Bit PlatformsUploaded byKemploe
- G52CPP_2012 Lecture 2 FinalUploaded bykrishna sharma
- An Introduction to Java Programming for First-time Programmers - Java Programming TutorialUploaded byBesoEl-Gendy
- c-manual 14Uploaded byscmiguel
- 02 SlideUploaded byAnonymous ZntoXci
- Cisco Bts 10200 Soft SwitchUploaded byMuhammad Delowar Hossain
- MIS387-3Uploaded byravg10
- Summary Primitive Types and ExpressionsUploaded bysaracatunga
- spec_test_1_for_BBUploaded byKieren Shepherd
- L02Uploaded byhisuin
- j2me labUploaded byMadhu
- 4.MD.4 Task 3Uploaded byLah Tahzan
- Excel Text Function 2Uploaded byArslan Saleem
- MELJUN CORTES Programming Languages DeclarationsUploaded byMELJUN CORTES, MBA,MPA
- Python_ProgrammingUploaded byapi-26155224
- Quiz06 SolUploaded byPascal
- Programming Basics - C++Uploaded bybenzenten
- Csharp VariablesUploaded byInderjeet Singh
- ISCC DumpUploaded byawdx159753
- 1 D ArraysUploaded byMuhammad Akif Naeem
- 3pi-Linefollower - Demo Code for the Pololu 3pi RobotUploaded byRizal Febrian
- GeoprocessorUploaded bycristiandub
- Assignment of C++ of AUSTUploaded byNazmul Hasan Pias
- An Introduction to Digital Image Processing with MatlabUploaded byDr Abubakar
- 2F_VERI_V10Uploaded byrewwedeiur

- Hdfc Statment 2018 2019Uploaded byDelhi Police
- GSIS_ES2012-1Uploaded byMaribeth Arandia
- Using the Transient Plane Source Method for Measuring Thermal Parameters of ElectroceramicsUploaded byKevonSingh1
- Coursework#1_Mining Equip.pdfUploaded byNaresh Narine
- CentOS Group 1Uploaded byNiel Jon Carl Aguel
- Nanog - Bgp Made SimpleUploaded byinnovativekalu
- CMP Controls Catalogue 2014Uploaded byJohn Evangelista
- Dense Phase Pneumatic Conveying System, Pneumatic Conveying Regimes, Solid Dense Conveying Systems, Mumbai, IndiaUploaded byRajarajan Krishnasamy
- Regenerative Braking System - ModifiedUploaded bySkive Fabsys
- Start-Up Commissioning Specialist Compressor Station Operator Global Resume Kevin SheldonUploaded byKevinSheldon
- 1.Laganapan vs. AsedilloUploaded byJuliefe Cuenca
- HE300 Brief eUploaded bycoolmarwin123
- STO1Uploaded bysatishnettmi1
- PERFORMANCE ANALYSIS OF FLS, EXP, LOG AND M-LWDF PACKET SCHEDULING ALGORITHMS IN DOWNLINK 3GPP LTE SYSTEMUploaded byJohn Berg
- Steel SizesUploaded bylesmaz
- Python Mining ConsultantsUploaded bygriverac
- BP CRM Scenario BB MatrixUploaded bySanjeev Joshi
- AirtronicD2+D4 TechnicalUploaded bytubophile
- Alice Corporation Pty. Ltd. v. CLS Bank International and CLS Services Ltd.Uploaded byRachel Paulose
- ManUploaded byCharles Amaechi
- Jet Grouting 03-13Uploaded byberk
- Settings Canon Xf 305Uploaded bycel_batran
- Comprehensive List of Tools 2014Uploaded bySmart Campaign
- Chapter 8 sprijinireUploaded bySima Viorel
- PPI Final Demand and Commodity Market TipsUploaded bysreelu42
- DH-1118Uploaded byThe Delphos Herald
- Trademark NotesUploaded byLuizaBalan
- Distribution Code v 18r1Uploaded bylalvith04
- Asian Paint Strategic ManagementUploaded byarushpratyush
- Caro Audit ReportUploaded byAfroz Ahmed