Computer Science Homework Help

Programming Homework Help
COMPUTER SCIENCE ASSIGNMENT HELP
For any help regarding Computer Science Assignment Help
Visit : - https://www.programminghomeworkhelp.com/ ,
Email : - support@programminghomeworkhelp.com or
call us at : - +1 678 648 4277
programminghomeworkhelp.com
Problem 1 - Login security
One important aspect of security in computer science is the concept of hashing: taking some
text, and somehow converting it to a number. This is needed because many security algorithms
work through math, so numbers are needed.
Another important aspect is the use of the modulo operator (%). You've seen this -- it returns
the remainder portion of a division. This is useful because unlike most other math operators,
modulo is one-way. That is, I can tell you that I'm thinking of a number x, and when I mod it by
5, I get 3, but from this information alone, you don't know whether x is 3 or 8 or 13 or 18, or ...
In this problem, we'll create a login screen, where the user must enter a password in order to
see a secret message. We will give the user 3 chances to get the password right, and either
print the secret message or a failure message (after 3 chances).
First, define a function encrypt that takes one string. It will hash the string using the built-in
Python function hash (try it on the shell) and modulo the value by a prime number (e.g. 541 --
this is very small in the computer science world but good enough for us). The function should
then return this number.
e.g. encrypt("mypassword") -> 283 (if you use 541 as the prime, for example) At the top
of the file, define a variable _KEY to be the result, e.g. _KEY = 283.
Now, write the rest of the program. Each time you ask the user for the password, call encrypt
with the user's input, and compare the value to _KEY. If the two match, the user (most likely)
entered the correct password, otherwise he loses one chance.
(see back for Problem 2)
pROBLEMS
programminghomeworkhelp.com
Problem 2 - The game of Nims / Stones
In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones
(assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins.
Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we
have to use nested loops (one loop inside another).
In the outermost loop, we want to keep playing until we are out of stones.
Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping
a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's
definitely do-able!
Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid
number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are
no, we should tell the user and re-ask them the question.
So, the basic outline of the program should be something like this:
TOTAL = 100
MAX = 5
pile = TOTAL # all stones are in the pile to start while [pile is not empty]:
while [player 1's answer is not valid]: [ask player 1]
[check player 1's input... is it valid?]
[same as above for player 2]
Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it
allows you to easily change the constants of a program. For example, for testing, you may want to start with only
15 or 20 stones.
Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their
answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a
variable that tracks whether their answer is valid, and set it to False initially.
When you're finished, test each other's programs by playing them! programminghomeworkhelp.com
# login.py
# Example solution for Lab 4, problem 1
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 3
# Some constants...
LARGE_PRIME = 541
_KEY = 171 # to get this number, I used the password "solution"
MAX_FAILURES = 3 # stop when we hit this many failures
# The encrypt function. Remember, functions shouldn't be asking for input or
# printing their result. Any input a function needs (in this case, a string to
# encrypt) should be passed in, and the output should be returned.
def encrypt(text):
return hash(text) % LARGE_PRIME
# Main program code
num_failures = 0
SOLUTIONS
programminghomeworkhelp.com
# We'll keep looping until we hit the max number of failures...
# We need to break out of the loop when we get it correct also, see below.
while num_failures < MAX_FAILURES:
login = raw_input("Please enter the password: ")
if encrypt(login) == _KEY:
print "Correct!"
break # remember, this breaks out of the current loop
else:
num_failures = num_failures + 1
print "Incorrect! You have failed", num_failures, "times."
# When we get here, it's either because num_failures == MAX_FAILURES, or
# because we hit the break statement (i.e. we got the correct login), so...
if num_failures >= MAX_FAILURES:
print "Sorry, you have hit the maximum number of failures allowed."
#
# Mihir Kedia
# Homework 3 - Nim
#
# Notes: This program will crash if someone enters a non-integer at the
prompt.
# We haven't learned how to fix this yet.
# programminghomeworkhelp.com
print ' ____...----....'
print ' |""--..__...------._'
print ' `""--..__|.__ `.'
print ' | ` |'
print ' | | |'
print ' / | |'
print ' __' / |'
print ' ,' ""--..__ ,-' |'
print '|""--..__ ,' |'
print '| ""| |'
print '| ; , | |'
print '| ' | |"'
print '| | |.-.___'
print '| | | "---(='
print '|__ | __..''
print ' ""--..__|__..--""'
print
print "Welcome to Nim!"
print
print "The rules of this game are simple. You start
with a pile of stones.", 
"Each player takes turns removing between 1
and 5 stones from the pile.", 
"The player who removes the last stone wins!"
print
print
programminghomeworkhelp.com
#get a valid initial pile size
pile_size = int(raw_input("How many stones would you like to start with?n"))
while pile_size <= 0:
pile_size = int(raw_input("You need to start with at least one stone in the
pile!n"))
#2 players; player 1 and player 2. Start with player 1
player = 1
#main game loop
while pile_size > 0:
prompt = "Player " + str(player) + ", there are " + str(pile_size) + " stones in
front of you. " + 
"How many stones would you like to remove (1-5)?n"
move = int(raw_input(prompt))
if move <= 0:
print "Hey! You have to remove at least one stone!"
elif move > pile_size:
print "There aren't even that many stones in the pile..."
elif move > 5:
print "You can't remove more than five stones.“
else:
#if we're here, they gave a valid move
pile_size = pile_size - move
player = 2-(player-1) #this is kind of cute: changes 1 to 2 and 2 to 1.
#If we've exited the loop, the pile size is 0. The player whose turn it is NOW
just lost the game..
print "Player", 2-(player-1), "has won the game!"
programminghomeworkhelp.com
# nims.py
# Example solution for Lab 4, problem 2.
#
# Aseem Kishore
#
# 6.189 - Intro to Python
# IAP 2008 - Class 3
TOTAL = 100
MAX = 5
pile = TOTAL # number of stones in the pile at any given time
def is_valid(x):
# returns True iff x is between 1 and MAX, and there's also enough stones,
# otherwise returns False
return (x >= 1) and (x <= MAX) and (x <= pile)
while pile > 0:
# player 1 turn
print "Player 1's turn. There are", pile, "stones in the pile."
x = 0
while not is_valid(x):
programminghomeworkhelp.com
# ask
x = int(raw_input("Player 1, how many? "))
# check
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",
"you can't pick up more than there are in the pile."
pile = pile - x
if pile == 0:
# win -- do something
print "Congratulations, Player 1, you win!"
break
# player 2 turn
print "Player 2's turn. There are", pile, "stones in the pile."
y = 0
while not is_valid(y):
y = int(raw_input("Player 2, how many? "))
if not is_valid(x):
print "That's not valid, it has to be between 1 and 5, and",
"you can't pick up more than there are in the pile."
pile = pile - y
if pile == 0:
# win -- do something
print "Congratulations, Player 2, you win!"
break
print "Game over."
programminghomeworkhelp.com
1 de 9

Recomendados

Python Homework Help por
Python Homework HelpPython Homework Help
Python Homework HelpProgramming Homework Help
77 vistas17 diapositivas
Introduction to Python decorators por
Introduction to Python decoratorsIntroduction to Python decorators
Introduction to Python decoratorsrikbyte
2.2K vistas16 diapositivas
Effective Java with Groovy - How Language Influences Adoption of Good Practices por
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good PracticesNaresha K
294 vistas75 diapositivas
JFokus 50 new things with java 8 por
JFokus 50 new things with java 8JFokus 50 new things with java 8
JFokus 50 new things with java 8José Paumard
2.5K vistas177 diapositivas
Java SE 8 for Java EE developers por
Java SE 8 for Java EE developersJava SE 8 for Java EE developers
Java SE 8 for Java EE developersJosé Paumard
4.8K vistas160 diapositivas
Linked to ArrayList: the full story por
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full storyJosé Paumard
2.6K vistas127 diapositivas

Más contenido relacionado

La actualidad más candente

Python decorators por
Python decoratorsPython decorators
Python decoratorsGuillermo Blasco Jiménez
1.1K vistas38 diapositivas
P4 2018 io_functions por
P4 2018 io_functionsP4 2018 io_functions
P4 2018 io_functionsProf. Wim Van Criekinge
1.7K vistas27 diapositivas
Zend Certification Preparation Tutorial por
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation TutorialLorna Mitchell
24.3K vistas214 diapositivas
lab4_php por
lab4_phplab4_php
lab4_phptutorialsruby
349 vistas5 diapositivas
DRYing to Monad in Java8 por
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
14.1K vistas80 diapositivas
An introduction to Raku por
An introduction to RakuAn introduction to Raku
An introduction to RakuSimon Proctor
2.9K vistas55 diapositivas

La actualidad más candente(20)

Zend Certification Preparation Tutorial por Lorna Mitchell
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
Lorna Mitchell24.3K vistas
DRYing to Monad in Java8 por Dhaval Dalal
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
Dhaval Dalal14.1K vistas
An introduction to Raku por Simon Proctor
An introduction to RakuAn introduction to Raku
An introduction to Raku
Simon Proctor2.9K vistas
Perl.Hacks.On.Vim por Lin Yo-An
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An107.6K vistas
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur... por Fwdays
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur..."How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
"How was it to switch from beautiful Perl to horrible JavaScript", Viktor Tur...
Fwdays234 vistas
Learning puppet chapter 2 por Vishal Biyani
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2
Vishal Biyani5.1K vistas
The Django Book / Chapter 3: Views and URLconfs por Vincent Chien
The Django Book / Chapter 3: Views and URLconfsThe Django Book / Chapter 3: Views and URLconfs
The Django Book / Chapter 3: Views and URLconfs
Vincent Chien649 vistas
Elegant Solutions For Everyday Python Problems - Nina Zakharenko por Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina ZakharenkoElegant Solutions For Everyday Python Problems - Nina Zakharenko
Elegant Solutions For Everyday Python Problems - Nina Zakharenko
Nina Zakharenko3.9K vistas
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko por Nina Zakharenko
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina ZakharenkoElegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko
Elegant Solutions for Everyday Python Problems Pycon 2018 - Nina Zakharenko
Nina Zakharenko10.9K vistas
JavaScript Functions por Colin DeCarlo
JavaScript FunctionsJavaScript Functions
JavaScript Functions
Colin DeCarlo2.7K vistas
Elegant Solutions For Everyday Python Problems - PyCon Canada 2017 por Nina Zakharenko
Elegant Solutions For Everyday Python Problems - PyCon Canada 2017Elegant Solutions For Everyday Python Problems - PyCon Canada 2017
Elegant Solutions For Everyday Python Problems - PyCon Canada 2017
Nina Zakharenko3.2K vistas
Programming in Computational Biology por AtreyiB
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
AtreyiB8K vistas
Mocking Dependencies in PHPUnit por mfrost503
Mocking Dependencies in PHPUnitMocking Dependencies in PHPUnit
Mocking Dependencies in PHPUnit
mfrost5039K vistas
Functional Javascript por guest4d57e6
Functional JavascriptFunctional Javascript
Functional Javascript
guest4d57e61.9K vistas

Similar a Computer Science Homework Help

Python Homework Help por
Python Homework HelpPython Homework Help
Python Homework HelpPython Homework Help
11 vistas11 diapositivas
Python Homework Help por
Python Homework HelpPython Homework Help
Python Homework HelpPython Homework Help
4 vistas11 diapositivas
homework and lab for matlab 243.docx por
homework and lab for matlab 243.docxhomework and lab for matlab 243.docx
homework and lab for matlab 243.docxscottharry3
3 vistas2 diapositivas
homework and lab for matlab 243.docx por
homework and lab for matlab 243.docxhomework and lab for matlab 243.docx
homework and lab for matlab 243.docx4934bk
2 vistas2 diapositivas
#In this project you will write a program play TicTacToe #using tw.pdf por
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquacareser
4 vistas17 diapositivas
#In this project you will write a program play TicTacToe #using tw.pdf por
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdfaquapariwar
2 vistas17 diapositivas

Similar a Computer Science Homework Help(20)

homework and lab for matlab 243.docx por scottharry3
homework and lab for matlab 243.docxhomework and lab for matlab 243.docx
homework and lab for matlab 243.docx
scottharry33 vistas
homework and lab for matlab 243.docx por 4934bk
homework and lab for matlab 243.docxhomework and lab for matlab 243.docx
homework and lab for matlab 243.docx
4934bk2 vistas
#In this project you will write a program play TicTacToe #using tw.pdf por aquacareser
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
aquacareser4 vistas
#In this project you will write a program play TicTacToe #using tw.pdf por aquapariwar
#In this project you will write a program play TicTacToe #using tw.pdf#In this project you will write a program play TicTacToe #using tw.pdf
#In this project you will write a program play TicTacToe #using tw.pdf
aquapariwar2 vistas
explain this code in extreme detail. what each function and line is .pdf por sales98
explain this code in extreme detail. what each function and line is .pdfexplain this code in extreme detail. what each function and line is .pdf
explain this code in extreme detail. what each function and line is .pdf
sales982 vistas
Java Guessing Game Number Tutorial por OXUS 20
Java Guessing Game Number TutorialJava Guessing Game Number Tutorial
Java Guessing Game Number Tutorial
OXUS 202.5K vistas
Python for High School Programmers por Siva Arunachalam
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam1.9K vistas
ch05-program-logic-indefinite-loops.ppt por Mahyuddin8
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
Mahyuddin821 vistas
Most asked JAVA Interview Questions & Answers. por Questpond
Most asked JAVA Interview Questions & Answers.Most asked JAVA Interview Questions & Answers.
Most asked JAVA Interview Questions & Answers.
Questpond40 vistas
Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx por randymartin91030
Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docxPhase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx
Phase 4 Problem Solving with LoopsTotal orderDescriptionOnce .docx
randymartin910304 vistas
#Note This problem will be in python # The code below is a guess.pdf por hanumanparsadhsr
#Note This problem will be in python # The code below is a guess.pdf#Note This problem will be in python # The code below is a guess.pdf
#Note This problem will be in python # The code below is a guess.pdf
hanumanparsadhsr2 vistas
You will write a multi-interface version of the well-known concentra.pdf por FashionColZone
You will write a multi-interface version of the well-known concentra.pdfYou will write a multi-interface version of the well-known concentra.pdf
You will write a multi-interface version of the well-known concentra.pdf
FashionColZone2 vistas
In C++, write a game of Are You Smarter than a 5th grader. For thi.pdf por siva009113
In C++, write a game of Are You Smarter than a 5th grader. For thi.pdfIn C++, write a game of Are You Smarter than a 5th grader. For thi.pdf
In C++, write a game of Are You Smarter than a 5th grader. For thi.pdf
siva0091134 vistas
Here is the question- You will write a program that simulates a game c (1).docx por Isaac9LjWelchq
Here is the question- You will write a program that simulates a game c (1).docxHere is the question- You will write a program that simulates a game c (1).docx
Here is the question- You will write a program that simulates a game c (1).docx
Isaac9LjWelchq3 vistas
Could someone please tell me why this C code will not run- The errors.docx por CharlesXMLAllano
Could someone please tell me why this C code will not run- The errors.docxCould someone please tell me why this C code will not run- The errors.docx
Could someone please tell me why this C code will not run- The errors.docx
CharlesXMLAllano3 vistas
Artificial intelligence - python por Sunjid Hasan
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
Sunjid Hasan148 vistas
Learn python por mocninja
Learn pythonLearn python
Learn python
mocninja1.7K vistas

Más de Programming Homework Help

Best Algorithms Assignment Help por
Best Algorithms Assignment Help Best Algorithms Assignment Help
Best Algorithms Assignment Help Programming Homework Help
14 vistas19 diapositivas
Algorithm Homework Help por
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework HelpProgramming Homework Help
19 vistas33 diapositivas
Algorithm Homework Help por
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework HelpProgramming Homework Help
85 vistas26 diapositivas
Algorithms Design Assignment Help por
Algorithms Design Assignment HelpAlgorithms Design Assignment Help
Algorithms Design Assignment HelpProgramming Homework Help
44 vistas18 diapositivas
Algorithms Design Homework Help por
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework HelpProgramming Homework Help
51 vistas21 diapositivas
Algorithm Assignment Help por
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment HelpProgramming Homework Help
149 vistas20 diapositivas

Más de Programming Homework Help(20)

Último

ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively por
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyPECB
574 vistas18 diapositivas
Recap of our Class por
Recap of our ClassRecap of our Class
Recap of our ClassCorinne Weisgerber
74 vistas15 diapositivas
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptx por
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxGopal Chakraborty Memorial Quiz 2.0 Prelims.pptx
Gopal Chakraborty Memorial Quiz 2.0 Prelims.pptxDebapriya Chakraborty
625 vistas81 diapositivas
Use of Probiotics in Aquaculture.pptx por
Use of Probiotics in Aquaculture.pptxUse of Probiotics in Aquaculture.pptx
Use of Probiotics in Aquaculture.pptxAKSHAY MANDAL
95 vistas15 diapositivas
The basics - information, data, technology and systems.pdf por
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdfJonathanCovena1
106 vistas1 diapositiva
Computer Introduction-Lecture06 por
Computer Introduction-Lecture06Computer Introduction-Lecture06
Computer Introduction-Lecture06Dr. Mazin Mohamed alkathiri
82 vistas12 diapositivas

Último(20)

ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively por PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 574 vistas
Use of Probiotics in Aquaculture.pptx por AKSHAY MANDAL
Use of Probiotics in Aquaculture.pptxUse of Probiotics in Aquaculture.pptx
Use of Probiotics in Aquaculture.pptx
AKSHAY MANDAL95 vistas
The basics - information, data, technology and systems.pdf por JonathanCovena1
The basics - information, data, technology and systems.pdfThe basics - information, data, technology and systems.pdf
The basics - information, data, technology and systems.pdf
JonathanCovena1106 vistas
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx por Inge de Waard
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptxOEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
OEB 2023 Co-learning To Speed Up AI Implementation in Courses.pptx
Inge de Waard169 vistas
11.28.23 Social Capital and Social Exclusion.pptx por mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239291 vistas
The Accursed House by Émile Gaboriau por DivyaSheta
The Accursed House  by Émile GaboriauThe Accursed House  by Émile Gaboriau
The Accursed House by Émile Gaboriau
DivyaSheta187 vistas
Class 10 English lesson plans por TARIQ KHAN
Class 10 English  lesson plansClass 10 English  lesson plans
Class 10 English lesson plans
TARIQ KHAN280 vistas
REPRESENTATION - GAUNTLET.pptx por iammrhaywood
REPRESENTATION - GAUNTLET.pptxREPRESENTATION - GAUNTLET.pptx
REPRESENTATION - GAUNTLET.pptx
iammrhaywood91 vistas
7 NOVEL DRUG DELIVERY SYSTEM.pptx por Sachin Nitave
7 NOVEL DRUG DELIVERY SYSTEM.pptx7 NOVEL DRUG DELIVERY SYSTEM.pptx
7 NOVEL DRUG DELIVERY SYSTEM.pptx
Sachin Nitave59 vistas
Class 10 English notes 23-24.pptx por TARIQ KHAN
Class 10 English notes 23-24.pptxClass 10 English notes 23-24.pptx
Class 10 English notes 23-24.pptx
TARIQ KHAN125 vistas
Structure and Functions of Cell.pdf por Nithya Murugan
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan455 vistas
Dance KS5 Breakdown por WestHatch
Dance KS5 BreakdownDance KS5 Breakdown
Dance KS5 Breakdown
WestHatch69 vistas
11.30.23 Poverty and Inequality in America.pptx por mary850239
11.30.23 Poverty and Inequality in America.pptx11.30.23 Poverty and Inequality in America.pptx
11.30.23 Poverty and Inequality in America.pptx
mary850239149 vistas
Are we onboard yet University of Sussex.pptx por Jisc
Are we onboard yet University of Sussex.pptxAre we onboard yet University of Sussex.pptx
Are we onboard yet University of Sussex.pptx
Jisc93 vistas
Narration lesson plan.docx por TARIQ KHAN
Narration lesson plan.docxNarration lesson plan.docx
Narration lesson plan.docx
TARIQ KHAN108 vistas
Lecture: Open Innovation por Michal Hron
Lecture: Open InnovationLecture: Open Innovation
Lecture: Open Innovation
Michal Hron99 vistas

Computer Science Homework Help

  • 1. COMPUTER SCIENCE ASSIGNMENT HELP For any help regarding Computer Science Assignment Help Visit : - https://www.programminghomeworkhelp.com/ , Email : - support@programminghomeworkhelp.com or call us at : - +1 678 648 4277 programminghomeworkhelp.com
  • 2. Problem 1 - Login security One important aspect of security in computer science is the concept of hashing: taking some text, and somehow converting it to a number. This is needed because many security algorithms work through math, so numbers are needed. Another important aspect is the use of the modulo operator (%). You've seen this -- it returns the remainder portion of a division. This is useful because unlike most other math operators, modulo is one-way. That is, I can tell you that I'm thinking of a number x, and when I mod it by 5, I get 3, but from this information alone, you don't know whether x is 3 or 8 or 13 or 18, or ... In this problem, we'll create a login screen, where the user must enter a password in order to see a secret message. We will give the user 3 chances to get the password right, and either print the secret message or a failure message (after 3 chances). First, define a function encrypt that takes one string. It will hash the string using the built-in Python function hash (try it on the shell) and modulo the value by a prime number (e.g. 541 -- this is very small in the computer science world but good enough for us). The function should then return this number. e.g. encrypt("mypassword") -> 283 (if you use 541 as the prime, for example) At the top of the file, define a variable _KEY to be the result, e.g. _KEY = 283. Now, write the rest of the program. Each time you ask the user for the password, call encrypt with the user's input, and compare the value to _KEY. If the two match, the user (most likely) entered the correct password, otherwise he loses one chance. (see back for Problem 2) pROBLEMS programminghomeworkhelp.com
  • 3. Problem 2 - The game of Nims / Stones In this game, two players sit in front of a pile of 100 stones. They take turns, each removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile). The person who removes the last stone(s) wins. Write a program to play this game. This may seem tricky, so break it down into parts. Like many programs, we have to use nested loops (one loop inside another). In the outermost loop, we want to keep playing until we are out of stones. Inside that, we want to keep alternating players. You have the option of either writing two blocks of code, or keeping a variable that tracks the current player. The second way is slightly trickier since we haven't learned lists yet, but it's definitely do-able! Finally, we might want to have an innermost loop that checks if the user's input is valid. Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in the pile to take off this many? If any of these answers are no, we should tell the user and re-ask them the question. So, the basic outline of the program should be something like this: TOTAL = 100 MAX = 5 pile = TOTAL # all stones are in the pile to start while [pile is not empty]: while [player 1's answer is not valid]: [ask player 1] [check player 1's input... is it valid?] [same as above for player 2] Note how the important numbers 100 and 5 are stored in a single variable at the top. This is good practice -- it allows you to easily change the constants of a program. For example, for testing, you may want to start with only 15 or 20 stones. Be careful with the validity checks. Specifically, we want to keep asking player 1 for their choice as long as their answer is not valid, BUT we want to make sure we ask them at least ONCE. So, for example, we will want to keep a variable that tracks whether their answer is valid, and set it to False initially. When you're finished, test each other's programs by playing them! programminghomeworkhelp.com
  • 4. # login.py # Example solution for Lab 4, problem 1 # # Aseem Kishore # # 6.189 - Intro to Python # IAP 2008 - Class 3 # Some constants... LARGE_PRIME = 541 _KEY = 171 # to get this number, I used the password "solution" MAX_FAILURES = 3 # stop when we hit this many failures # The encrypt function. Remember, functions shouldn't be asking for input or # printing their result. Any input a function needs (in this case, a string to # encrypt) should be passed in, and the output should be returned. def encrypt(text): return hash(text) % LARGE_PRIME # Main program code num_failures = 0 SOLUTIONS programminghomeworkhelp.com
  • 5. # We'll keep looping until we hit the max number of failures... # We need to break out of the loop when we get it correct also, see below. while num_failures < MAX_FAILURES: login = raw_input("Please enter the password: ") if encrypt(login) == _KEY: print "Correct!" break # remember, this breaks out of the current loop else: num_failures = num_failures + 1 print "Incorrect! You have failed", num_failures, "times." # When we get here, it's either because num_failures == MAX_FAILURES, or # because we hit the break statement (i.e. we got the correct login), so... if num_failures >= MAX_FAILURES: print "Sorry, you have hit the maximum number of failures allowed." # # Mihir Kedia # Homework 3 - Nim # # Notes: This program will crash if someone enters a non-integer at the prompt. # We haven't learned how to fix this yet. # programminghomeworkhelp.com
  • 6. print ' ____...----....' print ' |""--..__...------._' print ' `""--..__|.__ `.' print ' | ` |' print ' | | |' print ' / | |' print ' __' / |' print ' ,' ""--..__ ,-' |' print '|""--..__ ,' |' print '| ""| |' print '| ; , | |' print '| ' | |"' print '| | |.-.___' print '| | | "---(=' print '|__ | __..'' print ' ""--..__|__..--""' print print "Welcome to Nim!" print print "The rules of this game are simple. You start with a pile of stones.", "Each player takes turns removing between 1 and 5 stones from the pile.", "The player who removes the last stone wins!" print print programminghomeworkhelp.com
  • 7. #get a valid initial pile size pile_size = int(raw_input("How many stones would you like to start with?n")) while pile_size <= 0: pile_size = int(raw_input("You need to start with at least one stone in the pile!n")) #2 players; player 1 and player 2. Start with player 1 player = 1 #main game loop while pile_size > 0: prompt = "Player " + str(player) + ", there are " + str(pile_size) + " stones in front of you. " + "How many stones would you like to remove (1-5)?n" move = int(raw_input(prompt)) if move <= 0: print "Hey! You have to remove at least one stone!" elif move > pile_size: print "There aren't even that many stones in the pile..." elif move > 5: print "You can't remove more than five stones.“ else: #if we're here, they gave a valid move pile_size = pile_size - move player = 2-(player-1) #this is kind of cute: changes 1 to 2 and 2 to 1. #If we've exited the loop, the pile size is 0. The player whose turn it is NOW just lost the game.. print "Player", 2-(player-1), "has won the game!" programminghomeworkhelp.com
  • 8. # nims.py # Example solution for Lab 4, problem 2. # # Aseem Kishore # # 6.189 - Intro to Python # IAP 2008 - Class 3 TOTAL = 100 MAX = 5 pile = TOTAL # number of stones in the pile at any given time def is_valid(x): # returns True iff x is between 1 and MAX, and there's also enough stones, # otherwise returns False return (x >= 1) and (x <= MAX) and (x <= pile) while pile > 0: # player 1 turn print "Player 1's turn. There are", pile, "stones in the pile." x = 0 while not is_valid(x): programminghomeworkhelp.com
  • 9. # ask x = int(raw_input("Player 1, how many? ")) # check if not is_valid(x): print "That's not valid, it has to be between 1 and 5, and", "you can't pick up more than there are in the pile." pile = pile - x if pile == 0: # win -- do something print "Congratulations, Player 1, you win!" break # player 2 turn print "Player 2's turn. There are", pile, "stones in the pile." y = 0 while not is_valid(y): y = int(raw_input("Player 2, how many? ")) if not is_valid(x): print "That's not valid, it has to be between 1 and 5, and", "you can't pick up more than there are in the pile." pile = pile - y if pile == 0: # win -- do something print "Congratulations, Player 2, you win!" break print "Game over." programminghomeworkhelp.com