Skip to content
This repository was archived by the owner on Jun 4, 2019. It is now read-only.

some python challenges #37

Merged
merged 1 commit into from
Jul 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions challenges/1.1.Print/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Print
- http://www.python-course.eu/python3_print.php
The print() function is used to present the output of a program.
```
>>> print(“Hello World”)
Hello World
```
It can have several parameters.
```
>>> print(‘Hello’, 12, ‘98’)
Hello 12 98
```
The arguments of the print function:
sep=’’
Sep is a separator, it is used to divide parameters.
```
>>> print(‘Hello’, “World’, sep=’-‘)
Hello-World
```
We can assign an string to the keyword parameter "end". This string will be used for ending the output of the values of a print call.
end=’’
```
>>> print(‘Hello’, ‘World’, end=’!’)
Hello World?
```
By redefining the keyword parameter "file" we can send the output into a different stream file.
file=’’
```
>>> fh = open("data.txt","w")
>> print("no", file=fh)
>>> fh.close()
```
**_Instructions:_**
**Put your name and sname to appropriate places, change '*' to parameters of separator '_' and in the end of the line '!'.**
10 changes: 10 additions & 0 deletions challenges/1.1.Print/lesson_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import unittest


class PrintTests(unittest.TestCase):
def test_main(self):
f = open('main.py')
lines = str(f.readlines())
f.close()
self.assertRegex(lines, "sep='_'", msg="_ mising")
self.assertRegex(lines, "end='!'", msg="! mising")
1 change: 1 addition & 0 deletions challenges/1.1.Print/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print('firstname', 'sname', sep='*', end='*') #change this line
20 changes: 20 additions & 0 deletions challenges/1.2.Escape_Characters/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Escape Character
The escape character is "\\" and this character is invoked an alternative interpretation in a character sequence.
- https://docs.python.org/2.0/ref/strings.html
```
>>> print('\\')
\
>>> print('\'')
'
>>> print('Helllo\nWorld')
Hello
World
```
We can use escape character to print hex value Unicode character.
```
>>> print('\uxxxx')
Л
```

**_Instructions:_**
**Use escape character "new line" in the string_escape**
7 changes: 7 additions & 0 deletions challenges/1.2.Escape_Characters/lesson_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import unittest
from main import *


class EscapeTests(unittest.TestCase):
def test_main(self):
self.assertRegex(repr(string_escape), r'\\n', msg='the string must contain "new line"')
4 changes: 4 additions & 0 deletions challenges/1.2.Escape_Characters/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
string_escape = 'Hello, this is escape string' # change this line

# change the string_escape, the string must contain "new line"
print(string_escape)
30 changes: 30 additions & 0 deletions challenges/6.1.Conditionals/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## if elif else

- https://docs.python.org/3/tutorial/controlflow.html

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. In Python zero and null is assumed as FALSE value.
```
if value == 'some':
print('yes')
```
An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
```
if value == 'some':
print('yes')
else:
print('no')
```

The elif statement allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.

```
if value == 'some':
print('yes')
elif value = 'value':
print('maybe')
else:
print('no')
```

**_Instructions_**
**Modify the value, so that program must print 'yes'.**
7 changes: 7 additions & 0 deletions challenges/6.1.Conditionals/lesson_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lesson_title": "Conditionals",
"chapter_number": 6,
"lesson_number": 1,
"id":"5965e5fd60d67217f0ce232f",
"repl": ""
}
7 changes: 7 additions & 0 deletions challenges/6.1.Conditionals/lesson_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import unittest
from main import *

class ConditionalsTests(unittest.TestCase):
def test_main(self):
self.assertIsInstance(value, str)
self.assertIs(value, 'y', "program must print 'yes'")
8 changes: 8 additions & 0 deletions challenges/6.1.Conditionals/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
value = 'some' #modify this line

if value == 'Y' or value == 'y':
print('yes')
elif value == 'N' or value == 'n':
print('no')
else:
print('error')
38 changes: 38 additions & 0 deletions challenges/7.1.Loops/lesson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## Loops (While)

- https://docs.python.org/3/tutorial/controlflow.html

A loop statement allows to execute a block of code multiple times. Python supplies two type of loops:
* while
* for

The syntax of a while loop is:

```
while condition:
statement
```

While loops repeat a target statement as long as a given condition is true.

```
count = 0
while count < 9:
print('Loop iteration: ', count)
count = count + 1
```

Python supports to have an else statement associated with a loop statement. The else statement is executed when the condition becomes false.

```
count = 0
while count < 9:
print('Loop iteration ', count)
count = count + 1
else:
print('loop end')
```

**_Instructions_**
**Modify the count, so that condition and switch_loop must become true.**
**Use else statement, to mark the end of a program and switch switch_end to value true.**
7 changes: 7 additions & 0 deletions challenges/7.1.Loops/lesson_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"lesson_title": "Loops",
"chapter_number": 7,
"lesson_number": 1,
"id":"597e1a1a634a36abfebcda38",
"repl": ""
}
7 changes: 7 additions & 0 deletions challenges/7.1.Loops/lesson_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import unittest
from main import *

class LoopsTests(unittest.TestCase):
def test_main(self):
self.assertTrue(switch_end)
self.assertTrue(switch_loop)
12 changes: 12 additions & 0 deletions challenges/7.1.Loops/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
count = 10 # Change this count
switch_loop = False
switch_end = False

while count < 9:
print('step ', count)
count = count + 1
switch_loop = True
# use else statements
print('loop end')
switch_end = True;