Code Wars – Strings
- March 11th, 2013
- By arlis.hoglen
- Write comment
Be sure to read the Code Wars rules here before starting. Then click on the link below to open a PDF with the problem.
Let us know how Code Wars worked for you by rating this post.
Archive for the ‘Computer Science’ Category
Be sure to read the Code Wars rules here before starting. Then click on the link below to open a PDF with the problem.
Let us know how Code Wars worked for you by rating this post.
Assignment 1: Watch one of the following videos at home (one is YouTube so it will not open at school).
Assignment 2: Leave a comment below this post with a pledge of what you will do to promote Computer Science this week. (40 pts)
Check this video out. How does it relate? The Raspberry Pi runs on Linux plus you can program to do fun stuff with Python!
http://www.wimp.com/raspberrypi/
If you are interested in persuing this as a 2nd semester project, let me know.
Things I Have Learned while studying Lists:
If you want to attain the Zen of Python, comment regarding something significant that you have learned in this unit. It can be short but must be well written and thoughtful. I will then award you the key. (10 pts)
As we were studying lists and the .join( ) method, I thought the syntax was odd. If you remember, we discussed that for a list such as,
>>> s = ['ratta', 'tata', 'tata', 'tat']
we could convert it to a string using .join( ) and a delimiter (in this case a space):
>>> ” “.join(t)
This would produce:
(“ratta tata tata tat”)
” “.join(t) looks odd to me. I thought it would should be t.join(” “). So I did some searching on the intertubes. I found my answer, not too shockingly, at stackoverflow.com.
Click the link, read the questions and responses then comment as to why you think ” “.join(t) was selected by Python developers to convert a list into a string.
Homework: 10 pts.
When creating a list, you can skip adding all the quotes around and commas between elements of the list. Let Python do it for you by creating a string then convering it to a list using the .split() method.
>>> cheeses= “cheddar gouda swiss”.split()
>>> cheeses
['cheddar', 'gouda', 'swiss']
Jon M. for his contribution
Read 10-1 thru 10-2 in Think Python.
After reading, add a comment (question or question) on this post regarding a question you have after the reading and doing examples or a comment which summarizes your understanding. Your comment must
Comments will account for 10% of your grade for this part of the unit.
We will do the examples in class tomorrow.
1st: Jon A. and Jon M. (19/20 pts)
2nd: Spencer B. and David M (19/20 pts: 2nd due to file saving technicality)
3rd: Devin S. and Kendall T. (16/20 pts)
4th: Hannah N. and Nicole J. (15/20 pts)
Problem 1: “Fill in the Blanks” (4 pts)
Write a program which:
Example 1:
secretString = “kingarthur”
variable2 = “_ _ _ _ _ _ _ _ _ _” (spaces added for visibility but not required for the problem)
Example 2:
secretString = “12345”
variable2 = “_ _ _ _ _” (spaces added for visibility but not required for the problem)
def cw02(secretString):
secretString = len(secretString) * “_”
print (secretString)
cw02(“foo”)
>>>cw02(foo)
_ _ _
(spaces added as visual separator only)
Problem 2: “String it Along” (3 pts)
Write a program which:
Example 1:
secretString = “spam”
Results:
spam
Example 2:
secretString = “foo”
Results:
foo
def cw01(secretNumber):
for i in secretNumber:
print (i)
>>> cw01(“123”)
1
2
3
Problem 3: “Number, Please” (5 pts)
Write a program which:
Example 1:
secretString = “foo”, correctCharacters= “o”
Results:
o
o
Example 2:
secretString = “56789″, correctCharacters = “57″
Results:
5
7
def cw03(secretString, correctCharacters):
for i in range(len(secretString)):
if secretString[i] in correctCharacters:
print (secretString[i])
>>>cw03(“foo”, “o”)
o
o
Problem 4: “Spacing Out” (8 pts)
Create a program which:
Example 1:
secretStirng = “01234″, correctCharacters = “24″
Results:
_ _ 2 _ 4
Example 2:
secretString = (knightswhosayni ), correctCharacters=”nis”
Results:
_ n i _ _ _ s _ _ _ s _ _ n i
def cw04(secretString, correctCharacters):
blanks=len(secretString) * “_”
for i in range(len(secretString)):
if secretString[i] in correctCharacters:
blanks=blanks[:i] + secretString[i] + blanks[i+1:]
for letter in blanks:
print (letter, end=” “)
cw04(“scope”, “op”)