Skip to content
This repository was archived by the owner on Aug 21, 2023. It is now read-only.

Commit 4066984

Browse files
tekkacBernardstanislas
authored andcommitted
feat(exercises): Add tricks track exercise
1 parent 9a91ec8 commit 4066984

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--- a/exercises/tricks/no_conditionals.cairo 2022-06-02 14:49:04.224066616 +0200
2+
+++ a/exercises/tricks/no_conditionals.cairo 2022-06-02 14:46:49.463087238 +0200
3+
@@ -28,2 +28,2 @@
4+
- # FILL ME
5+
- return (res)
6+
+ let (not_zero) = is_not_zero((x - 1) * x)
7+
+ return (res=1 - not_zero)
8+
@@ -37 +37,3 @@
9+
- # FILL ME
10+
+ let tmp = (x - 1337) * (x - 69420) * (x - 42)
11+
+ let (s) = is_not_zero(tmp)
12+
+ let res = s * 'meh' + (1 - s) * 'cool'
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
%lang starknet
2+
3+
from starkware.cairo.common.math_cmp import is_not_zero
4+
5+
# Sometimes, conditionals can be avoided by using an expression that
6+
# - maps valid inputs to 1,
7+
# - and/or maps invalid inputs to 0.
8+
# For instance (row - 2) * (row - 4) is 0 only when row is 2 or 4.
9+
10+
# Use this trick to rewrite functions without "if" conditions
11+
# Note: This helps to avoid dealing with revoked references.
12+
13+
# I AM NOT DONE
14+
15+
func is_binary_if(x : felt) -> (res : felt):
16+
if x == 0:
17+
return (1)
18+
end
19+
if x == 1:
20+
return (1)
21+
end
22+
return (0)
23+
end
24+
25+
# TODO: Return the right value to mimick the behavior of is_binary_if
26+
27+
func is_binary_no_if(x : felt) -> (res : felt):
28+
# FILL ME
29+
return (res)
30+
end
31+
32+
# TODO: Fix the function so that
33+
# - it returns the string 'cool' if x is 1337, 69420, 42
34+
# - it returns 'meh' on any other input
35+
36+
func is_cool(x : felt) -> (res : felt):
37+
# FILL ME
38+
return (res)
39+
end
40+
41+
# Do not change the test
42+
@external
43+
func test_is_binary{syscall_ptr : felt*}():
44+
let (eval_if) = is_binary_if(0)
45+
let (eval_no_if) = is_binary_no_if(0)
46+
assert (eval_if, eval_no_if) = (1, 1)
47+
48+
let (eval_if) = is_binary_if(1)
49+
let (eval_no_if) = is_binary_no_if(1)
50+
assert (eval_if, eval_no_if) = (1, 1)
51+
52+
let (eval_if) = is_binary_if(13)
53+
let (eval_no_if) = is_binary_no_if(37)
54+
assert (eval_if, eval_no_if) = (0, 0)
55+
return ()
56+
end
57+
58+
@external
59+
func test_is_cool{syscall_ptr : felt*}():
60+
let (is_1337_cool) = is_cool(1337)
61+
let (is_69420_cool) = is_cool(69420)
62+
let (is_42_cool) = is_cool(42)
63+
let (is_0_cool) = is_cool(0)
64+
let (is_911_cool) = is_cool(911)
65+
let results = ('cool', 'cool', 'cool', 'meh', 'meh')
66+
assert (is_1337_cool, is_69420_cool, is_42_cool, is_0_cool, is_911_cool) = results
67+
return ()
68+
end

src/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
),
2121
("exercises/registers", ["registers00", "registers01", "registers02"]),
2222
("exercises/revoked_references", ["revoked_references01"]),
23+
("exercises/tricks", ["no_conditionals"]),
2324
("exercises/hints", ["hints00", "hints01"]),
2425
]

0 commit comments

Comments
 (0)