14 views

- Kertas Pink
- MELSEC Q L F Series Structured Programming Manual Fundamentals
- Starting Out With Programming Logic & Design - Chapter6_Functions
- ABAP List Viewer Doc
- Java Report
- Introduction to Java Programming
- MBus_03_e
- Python All the Fun of the Fair
- Arduino Marimba Script 11-4-2015
- C_GROUP_009
- Basic of Programming Ass-1
- c-assignment.docx
- VB2
- Lab A
- Java Basic Datatypes 6
- Test1
- Ch 7 Data Handling
- 10262016032502
- Java Assignment 1
- 10.1.1.33

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))

- Kertas PinkUploaded byRyu8146
- MELSEC Q L F Series Structured Programming Manual FundamentalsUploaded byAng Ricardo
- Starting Out With Programming Logic & Design - Chapter6_FunctionsUploaded byAndrew Carts
- ABAP List Viewer DocUploaded byapi-3834175
- Java ReportUploaded byRally Cautiverio
- Introduction to Java ProgrammingUploaded byUser98765
- MBus_03_eUploaded byVoicu Stanese
- Python All the Fun of the FairUploaded bysdfg
- Arduino Marimba Script 11-4-2015Uploaded byRené Medina
- C_GROUP_009Uploaded byjairober
- Basic of Programming Ass-1Uploaded byArchana Saxena
- c-assignment.docxUploaded byLily Sharma
- VB2Uploaded byMasudRana
- Lab AUploaded bydRewsus
- Java Basic Datatypes 6Uploaded byJyotsana Sharma
- Test1Uploaded byKishore Babu
- Ch 7 Data HandlingUploaded bySanskruti Namjoshi
- 10262016032502Uploaded byTu Pham
- Java Assignment 1Uploaded byMahendra Pratap
- 10.1.1.33Uploaded byMajidadin Fazebook
- c LanguageUploaded byavula girish
- IOCARDs DRIVERversion 1.1Uploaded byOlivier Briot
- constants variablesUploaded byapi-288917372
- Ise 2 Introduction to C ProgrammingUploaded byMark Azzopardi
- CORBA DatatypesUploaded byAlex Beno
- F100Uploaded byKumaraguru Sathiyaseelan
- Strings and NumbersUploaded byyashwithgm
- Mid [CS100F17V2] SolutionUploaded bymuhammadriz
- CSC 1100 - Chapter 2 -Introduction to the C++ LanguageUploaded byMia Ezario
- JavaByteCodeStackISA.pdfUploaded byacer smith

- brochure Tractor Case.pdfUploaded byhmayda riad
- Arup_Journal_2_2012 (1)Uploaded byEhecatl Vientiuno Arquitectura
- microscope_4040,4050,4060Uploaded byMărian Ioan
- z0409mfUploaded byYadira Rodriguez
- A Street Car Named Desire (Penguin)Uploaded bythe_perfectionist
- 114-1086-1-PBUploaded bySofyan Rss
- IBRO News 2013Uploaded byInternational Brain Research Organization
- Lore on the Chinese Lute4Uploaded byPriyanka Mokkapati
- SDS-Uranium Oxide UO2Uploaded byAnonymous mZEUqu
- 3_remont_sceplenijUploaded byharshal161987
- 42155 Brunborg Main_thesisUploaded byMadalina Dragu
- Gem recoUploaded bydynamic2004
- Bs848 Fan Performance TestingUploaded byKumaran Gopal
- Nursing pathophysiology Study Guide Last Test (test 5)Uploaded byrainbowunicorn25
- Joyce's Early AestheticUploaded bySamBuckley
- Ppt 011Uploaded bykanggun0225
- MS-11Uploaded byKapil Dhingra
- Lagrange InterpolationUploaded byMihai Popescu
- Hitachi Circuit Breakers and Miniature Circuit BreakersUploaded byWilliamBradley Pitt
- Plato PhilebusUploaded bydekkn
- AMINOV L.pdf 4 14.pdfUploaded byBianca Diana
- Compassion Paster _ July 2009Uploaded bypacesoft321
- MIT8_02SC_notes19to20.pdfUploaded byGeorgeChang
- WordFast Training EnUploaded byArif Budiman
- Martin Gardner's 'The Annotated Alice' - NYTimes.pdfUploaded bybrownbag80
- Glogster Lesson PlanUploaded byAshley Clark
- 013 wireline tool stringUploaded bySoon Han Chean
- Quin AprilUploaded byBoyceLoc
- Copper Select Program2Uploaded bysajithv78
- ProjectAl-HussainiUploaded by김인겸