Skip to content

Commit

Permalink
Completion of the Simple Chatty Bot project
Browse files Browse the repository at this point in the history
  • Loading branch information
danfimov committed Jan 31, 2021
1 parent 10ba746 commit 77d814f
Show file tree
Hide file tree
Showing 21 changed files with 270 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def compare(numerator, denominator):
return (denominator != 0) and (numerator / denominator == 0.5)


a = int(input())
b = int(input())

print(compare(a, b))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type: code
files:
- name: main.py
visible: true
text: |-
def compare(numerator, denominator):
return denominator and numerator / denominator == 0.5
a = int(input())
b = int(input())
print(compare(a, b))
learner_created: false
feedback_link: https://hyperskill.org/learn/step/10333#comment
status: Solved
feedback:
message: <html>Correct solution</html>
time: Sun, 31 Jan 2021 10:06:06 UTC
record: -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: 10333
update_date: Mon, 21 Dec 2020 11:57:20 UTC
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>Correct a mistake</h2>
<html>
<head></head>
<body>
<p><span style="background-color: #ffffff; color: #222222; font-size: 12pt; font-variant: normal;"></span></p>
<p>The code below solves the following problem — having the numerator and the denominator of a fraction, check if the fraction equals <code class="language-python">0.5</code>. The program should print <code class="language-python">True</code> or <code class="language-python">False</code>. If the denominator equals <code class="language-python">0</code>, the answer is <code class="language-python">False</code>.</p>
<pre><code class="language-python">def compare(numerator, denominator):
return denominator and numerator / denominator == 0.5


a = int(input())
b = int(input())

print(compare(a, b))</code></pre>
<p>But this code works incorrectly. Find and correct the mistake, then run the code.</p>
</body>
</html><br><b>Sample Input:</b><br><pre><code class="language-no-highlight">5<br>10</code></pre><br><b>Sample Output:</b><br><pre><code class="language-no-highlight">True</code></pre><br><br><b>Sample Input:</b><br><pre><code class="language-no-highlight">3<br>0</code></pre><br><b>Sample Output:</b><br><pre><code class="language-no-highlight">False</code></pre><br><br><b>Sample Input:</b><br><pre><code class="language-no-highlight">-1<br>-2</code></pre><br><b>Sample Output:</b><br><pre><code class="language-no-highlight">True</code></pre><br><br><br><font color="gray">Memory limit: 256 MB</font><br><font color="gray">Time limit: 15 seconds</font><br><br>
<a href="https://hyperskill.org/learn/step/10327">Show topic summary</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def solve():
# Write your code here
print(wrong_password(False))
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: code
files:
- name: main.py
visible: true
text: |-
def solve():
# Write your code here
learner_created: false
feedback_link: https://hyperskill.org/learn/step/10334#comment
status: Solved
record: -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: 10334
update_date: Tue, 22 Dec 2020 14:56:30 UTC
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h2>Find the password</h2>
<html>
<head></head>
<body>
<p><span style="background-color: #ffffff; color: #222222; font-size: 12pt; font-variant: normal;"></span></p>
<p>Imagine that you are a hacker and you have got access to a web server written in Python. The problem is that it asks for a password that you do not know. However, this server is open source so you know that there is the <code class="language-python">wrong_password()</code> function which takes a password and checks it against <code class="language-python">real_password</code>.</p>
<pre><code class="language-python">def wrong_password(password):
return (password == "" or (not password and real_password)) or password != real_password</code></pre>
<p>You can not access the <code class="language-python">real_password</code> variable but you can call the <code class="language-python">wrong_password()</code> function and exploit its vulnerability to obtain the password. Write your code inside the <code class="language-python">solve()</code> function. Think about an argument that can be passed to the <code class="language-python">wrong_password()</code> function so that the function returns the <code class="language-python">real_password</code>. <strong>Print</strong> the result of the <code class="language-python">wrong_password()</code> function.</p>
<p>Your program should not read any input or call the <code class="language-python">solve()</code> function, your task is to implement it.</p>
<p><button class="btn-sm btn-outline-secondary" onclick="getElementById('hint-1263').style.display='inline'"> Hint </button> </p>
<div id="hint-1263" style="display:none;">
Remember that "and" and "or" operators return one of their operands. Refer to the tables showing which operands the operators return in different cases.
</div>
<p></p>
</body>
</html><br><br><font color="gray">Memory limit: 256 MB</font><br><font color="gray">Time limit: 15 seconds</font><br><br>
<b>Caution</b><br><br>
You may see errors in your code or execution results due to missing context. Don’t worry about it, just write the solution and press Check.
<br><br>
<a href="https://hyperskill.org/learn/step/10327">Show topic summary</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def xor(a, b):
if bool(a) == bool(b):
return False
else:
if a:
return a
else:
return b
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type: code
files:
- name: main.py
visible: true
text: |-
def xor(a, b):
# Write your code here
learner_created: false
feedback_link: https://hyperskill.org/learn/step/10332#comment
status: Solved
feedback:
message: <html>Correct solution</html>
time: Sun, 31 Jan 2021 10:12:44 UTC
record: -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id: 10332
update_date: Sat, 19 Dec 2020 03:04:30 UTC
27 changes: 27 additions & 0 deletions Python Developer/Simple Chatty Bot/Problems/XOR operator/task.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h2>XOR operator</h2>
<html>
<head></head>
<body>
<p><span style="background-color: #ffffff; color: #222222; font-size: 12pt; font-variant: normal;"></span></p>
<p>Implement an XOR operator that can work with objects of any type.</p>
<p>The behaviour should be the following:</p>
<ul>
<li>if the operands are both truthy or both falsy, <strong>return </strong><code class="java">False</code>,</li>
<li>if one operand is truthy and the other operand is falsy, <strong>return </strong>the <strong>truthy one</strong> (the operand itself, not <code class="java">True</code>).</li>
</ul>
<p>Write your code inside the <code class="java">xor()</code> function. Your program should not read any input or call the function, your task is to implement it.</p>
<p><button class="btn-sm btn-outline-secondary" onclick="getElementById('hint-713').style.display='inline'"> Hint </button> </p>
<div id="hint-713" style="display:none;">
Remember some facts: the
<code class="java">bool()</code> function returns
<code class="java">True</code> if the passed argument is truthy, and
<code class="java">False</code> if it is falsy. What operand does the
<code class="java">or</code> operator return?
</div>
<p></p>
</body>
</html><br><br><font color="gray">Memory limit: 256 MB</font><br><font color="gray">Time limit: 15 seconds</font><br><br>
<b>Caution</b><br><br>
You may see errors in your code or execution results due to missing context. Don’t worry about it, just write the solution and press Check.
<br><br>
<a href="https://hyperskill.org/learn/step/10327">Show topic summary</a>
4 changes: 4 additions & 0 deletions Python Developer/Simple Chatty Bot/Problems/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
content:
- Correct a mistake
- XOR operator
- Find the password
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ files:
# write your code here
learner_created: false
feedback_link: https://hyperskill.org/projects/97/stages/534/implement#comment
status: Unchecked
record: -1
status: Solved
feedback:
message: Congratulations!
time: Sun, 31 Jan 2021 09:53:35 UTC
record: 1
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ files:
print("Your age is {your_age}; that's a good time to start programming!")
learner_created: false
feedback_link: https://hyperskill.org/projects/97/stages/536/implement#comment
status: Unchecked
record: -1
status: Solved
feedback:
message: Congratulations!
time: Sun, 31 Jan 2021 09:56:41 UTC
record: 3
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@ files:
print('Completed, have a nice day!')
learner_created: false
feedback_link: https://hyperskill.org/projects/97/stages/537/implement#comment
status: Unchecked
record: -1
status: Solved
feedback:
message: Congratulations!
time: Sun, 31 Jan 2021 09:58:39 UTC
record: 4
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,8 @@ files:
end()
learner_created: false
feedback_link: https://hyperskill.org/projects/97/stages/538/implement#comment
status: Unchecked
status: Solved
feedback:
message: Congratulations!
time: Sun, 31 Jan 2021 10:01:26 UTC
record: -1
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ files:
print('What a great name you have, {your_name}!')
learner_created: false
feedback_link: https://hyperskill.org/projects/97/stages/535/implement#comment
status: Unchecked
record: -1
status: Solved
feedback:
message: Congratulations!
time: Sun, 31 Jan 2021 09:54:38 UTC
record: 2
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ content:
- Guess the age
- Learning numbers
- Multiple choice
current_task: 0
current_task: 4
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
# write your code here
def greet(bot_name, birth_year):
print('Hello! My name is ' + bot_name + '.')
print('I was created in ' + birth_year + '.')


def remind_name():
print('Please, remind me your name.')
name = input()
print('What a great name you have, ' + name + '!')


def guess_age():
print('Let me guess your age.')
print('Enter remainders of dividing your age by 3, 5 and 7.')

rem3 = int(input())
rem5 = int(input())
rem7 = int(input())
age = (rem3 * 70 + rem5 * 21 + rem7 * 15) % 105

print("Your age is " + str(age) + "; that's a good time to start programming!")


def count():
print('Now I will prove to you that I can count to any number you want.')

num = int(input())
curr = 0
while curr <= num:
print(curr, '!')
curr = curr + 1


def test():
print("Let's test your programming knowledge.")
print('')
print('''Why do we use methods?
1. To repeat a statement multiple times.
2. To decompose a program into several small subroutines.
3. To determine the execution time of a program.
4. To interrupt the execution of a program.''')
while (user_input := input()) != '4':
user_input = input('Please, try again.\n')
print('Completed, have a nice day!')


def end():
print('Congratulations, have a nice day!')


greet('Aid', '2020') # change it as you need
remind_name()
guess_age()
count()
test()
end()
56 changes: 49 additions & 7 deletions Python Developer/Simple Chatty Bot/Simple Chatty Bot/task/tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from hstest.stage_test import *
from hstest.test_case import TestCase

Expand All @@ -8,22 +7,65 @@

class ChattyBotTest(StageTest):
def generate(self) -> List[TestCase]:
return [TestCase()]
stdin = "Marry\n1\n0\n5\n10"
for i in range(10):
stdin += f'\n{i}'
return [
TestCase(stdin=stdin, attach=("Marry", 40, 10))
]

def check(self, reply: str, clue: Any) -> CheckResult:
lines = reply.strip().splitlines()
if len(lines) != 2:
length = 9 + clue[2] + 1
if len(lines) <= length:
return CheckResult.wrong(
"You should output exactly 2 lines!\n" +
f"You should output at least {length} lines " +
f"(for the count number {clue[2]}).\n" +
f"Lines found: {len(lines)}"
f"Your output:\n"
f"{reply.strip()}"
)

if not re.match(".*\\d.*", lines[1]):
line_with_name = lines[3].lower()
name = clue[0].lower()

if name not in line_with_name:
return CheckResult.wrong(
"The name was " + clue[0] + "\n" +
"But the 4-th line was:\n" +
"\"" + lines[3] + "\"\n\n" +
"4-th line should contain a name of the user"
)

line_with_age = lines[6].lower()
age = str(clue[1])

if age not in line_with_age:
return CheckResult.wrong(
"Can't find a correct age! " +
"Maybe you calculated the age wrong?\n\n" +
"Your line with age: \n" + "\"" + lines[6] + "\""
)

for i in range(clue[2] + 1):
num_line = lines[i + 8].strip().replace(' ', '')
actual_num = f'{i}!'

if num_line != actual_num:
return CheckResult.wrong(
f"Expected {i + 8}-th line: \n" +
f"\"{actual_num}\"\n" +
f"Your {i + 8}-th line: \n" +
f"\"{num_line}\""
)

last_line = lines[-1]
if "Congratulations, have a nice day!" != last_line:
return CheckResult.wrong(
"The second line should contain a year!\n" +
"Your second line: \"" + lines[1] + "\""
"Your last line should be:\n" +
"\"Congratulations, have a nice day!\"\n" +
"Found:\n" +
f"\"{last_line}\""
)

return CheckResult.correct()
Expand Down

0 comments on commit 77d814f

Please sign in to comment.