# triality 课程学习交互代码答案 **Published by:** [Untitled](https://paragraph.com/@0x30784ddcd2b71947a35120ee67a0a9771cdaa07b/) **Published on:** 2023-04-16 **URL:** https://paragraph.com/@0x30784ddcd2b71947a35120ee67a0a9771cdaa07b/triality ## Content 模块1徽章领取 可以直接做最后一题领取徽章 直接把原来的代码全部删除,把下面代码粘上去 //Construct your main function below this line func main() { let triangle_base_sides : felt = 3; let square_base_sides : felt = 4; assert triangle_base_sides = 3; assert square_base_sides = 4; let (triangle_base_area : felt) = get_ideal_base_area(triangle_base_sides); assert triangle_base_area = 300; let (square_base_area : felt) = get_ideal_base_area(square_base_sides); assert square_base_area = 400; let (comp_square_area : felt) = is_ideal_base_area(square_base_sides,square_base_area); assert comp_square_area = 1; let square_base_area_mod = 401; let (comp_square_area_mod : felt) = is_ideal_base_area(square_base_sides,square_base_area_mod); assert comp_square_area_mod = 0; return(); } //Write the appropriate functions below to ensure the assertions pass // do not modify code on this line or above func get_ideal_base_area(side : felt) -> (area : felt){ return(area = side * 100); } func is_ideal_base_area(side : felt ,area :felt) -> (is : felt){ if(side * 100 == area){ return (is=1); } return(is=0); } 点左边的验证按钮,答案通过以后右下角的next变边为可点击状态.点击next跳到领取页面. 模块二 1 - 5题 代码答案 如果想要领模块二的徽章,下面是其他的代码答案,注意,要是不做最后一个的话要把模块内剩余其他题全部做完,然后到最会一题点next才会跳转到领取页面. 1 //Complete the main function and any other needed functions so that the program compiles and runs successfully func main() { // do not modify code on this line or above tempvar quad_pyramid_slope_angles : felt* = new(51, 52, 51, 52); tempvar tri_pyramid_slope_angles : felt* = new(51, 52, 48); // do not modify code on this line or below assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; return(); } 2 //Complete the main function and any other needed functions so that the program compiles and runs successfully func main() { tempvar quad_pyramid_slope_angles : felt* = new (51,52,51,52); assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; let (is_quad_valid : felt) = verify_slopes(quad_pyramid_slope_angles, 4); assert is_quad_valid = 1; tempvar tri_pyramid_slope_angles : felt* = new (51,52,48); assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; let (is_tri_valid : felt) = verify_slopes(tri_pyramid_slope_angles, 3); return(); } // do not modify code on this line or above func verify_slopes(slopes_arr : felt*, slopes_len : felt) -> (is_valid : felt) { // For this exercise, we will always return true return(is_valid=1); } 3 这个最后的空行也要粘上去,不然验证按钮点不了 %builtins output from starkware.cairo.common.serialize import serialize_word func main{output_ptr : felt*}() { tempvar quad_pyramid_slope_angles : felt* = new (51,52,51,52); assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; let (is_quad_valid : felt) = verify_slopes(quad_pyramid_slope_angles, 4); assert is_quad_valid = 1; tempvar tri_pyramid_slope_angles : felt* = new (51,52,48); assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; let (is_tri_valid : felt) = verify_slopes(tri_pyramid_slope_angles, 3); // do not modify code on this line or above //add serialize word function below here for exploration tempvar my_variable : felt = 42; serialize_word(my_variable); // do not modify code on this line or below return(); } func verify_slopes(slopes_arr : felt*, slopes_len : felt) -> (is_valid : felt) { return(is_valid=1); } 4 %builtins output from starkware.cairo.common.serialize import serialize_word func main{output_ptr : felt*}() { tempvar quad_pyramid_slope_angles : felt* = new (51,52,51,52); assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; let (is_quad_valid : felt) = verify_slopes(quad_pyramid_slope_angles, 4); assert is_quad_valid = 1; tempvar tri_pyramid_slope_angles : felt* = new (51,52,48); assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; let (is_tri_valid : felt) = verify_slopes(tri_pyramid_slope_angles, 3); assert is_tri_valid = 0; serialize_word(is_tri_valid); return (); } // do not modify code on this line or above //Modify this function based on the prompt func verify_slopes(arr : felt*, len : felt) -> (is_valid : felt) { if (len == 0) { return (is_valid=1); } if (arr[0] != 51 and arr[0] != 52) { return (is_valid=0); } let (verify_res : felt) = verify_slopes(arr+1, len-1); return (is_valid=verify_res); } 5 %builtins output from starkware.cairo.common.serialize import serialize_word func main{output_ptr : felt*}() { alloc_locals; tempvar quad_pyramid_slope_angles : felt* = new (51,52,51,52); local quad_pyramid_slope_angles : felt* = quad_pyramid_slope_angles; assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; let (is_quad_valid : felt) = verify_slopes(quad_pyramid_slope_angles, 4); assert is_quad_valid = 1; tempvar tri_pyramid_slope_angles : felt* = new (51,52,48); assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; let (is_tri_valid : felt) = verify_slopes(tri_pyramid_slope_angles, 3); assert is_tri_valid = 0; let (double_verify_res : felt) = double_verify_slopes(quad_pyramid_slope_angles, 4, tri_pyramid_slope_angles, 3); assert double_verify_res = 0; return (); } func verify_slopes(slopes_arr : felt*, slopes_len : felt) -> (is_valid : felt) { if (slopes_len == 0) { return(is_valid=1); } if ((slopes_arr[0] - 51) * (slopes_arr[0] - 52) == 0) { return verify_slopes(slopes_arr+1, slopes_len-1); } return(is_valid=0); } // do not modify code on this line or above func double_verify_slopes(first_arr : felt*, first_arr_len : felt, second_arr : felt*, second_arr_len : felt) -> (res : felt) { alloc_locals; let (first_res : felt) = verify_slopes(first_arr, first_arr_len); let (second_res : felt) = verify_slopes(second_arr, second_arr_len); if (first_res == 1 and second_res == 1) { return (res=1); } return (res=0); } 6 %builtins output range_check from starkware.cairo.common.math_cmp import is_le from starkware.cairo.common.serialize import serialize_word func main{output_ptr : felt*, range_check_ptr}() { alloc_locals; tempvar quad_pyramid_slope_angles : felt* = new (51,52,51,52); local quad_pyramid_slope_angles : felt* = quad_pyramid_slope_angles; assert quad_pyramid_slope_angles[0] = 51; assert quad_pyramid_slope_angles[1] = 52; assert quad_pyramid_slope_angles[2] = 51; assert quad_pyramid_slope_angles[3] = 52; let (is_quad_valid : felt) = verify_slopes(quad_pyramid_slope_angles, 4); assert is_quad_valid = 1; tempvar tri_pyramid_slope_angles : felt* = new (51,52,48); assert tri_pyramid_slope_angles[0] = 51; assert tri_pyramid_slope_angles[1] = 52; assert tri_pyramid_slope_angles[2] = 48; let (is_tri_valid : felt) = verify_slopes(tri_pyramid_slope_angles, 3); assert is_tri_valid = 0; let (double_verify_res : felt) = double_verify_slopes(quad_pyramid_slope_angles, 4, tri_pyramid_slope_angles, 3); assert double_verify_res = 0; tempvar valid_tri_pyramid_slope_angles : felt* = new (51,52,52); let (is_tri_negative_pyramid : felt, is_tri_never_ending_pyramid : felt) = is_silly_pyramid(valid_tri_pyramid_slope_angles, 3); assert is_tri_negative_pyramid = 0; assert is_tri_never_ending_pyramid = 0; tempvar quad_full_obtuse_slope_angles : felt* = new (90,92,105,105); let (is_full_obtuse_negative_pyramid : felt, is_full_obtuse_never_ending_pyramid : felt) = is_silly_pyramid(quad_full_obtuse_slope_angles, 4); assert is_full_obtuse_negative_pyramid = 0; assert is_full_obtuse_never_ending_pyramid = 1; tempvar quad_full_negative_slope_angles : felt* = new (-1,-5,-10,-45); let (is_full_negative_negative_pyramid : felt, is_full_negative_never_ending_pyramid : felt) = is_silly_pyramid(quad_full_negative_slope_angles, 4); assert is_full_negative_negative_pyramid = 1; assert is_full_negative_never_ending_pyramid = 0; tempvar quad_partial_obtuse_slope_angles : felt* = new (90,92,89,105); let (is_partial_obtuse_negative_pyramid : felt, is_partial_obtuse_never_ending_pyramid : felt) = is_silly_pyramid(quad_partial_obtuse_slope_angles, 4); assert is_partial_obtuse_negative_pyramid = 0; assert is_partial_obtuse_never_ending_pyramid = 0; tempvar quad_partial_negative_slope_angles : felt* = new (-1,-5,10,-45); let (is_partial_negative_negative_pyramid : felt, is_partial_negative_never_ending_pyramid : felt) = is_silly_pyramid(quad_partial_negative_slope_angles, 4); assert is_partial_negative_negative_pyramid = 0; assert is_partial_negative_never_ending_pyramid = 0; return(); } func verify_slopes(slopes_arr : felt*, slopes_len : felt) -> (is_valid : felt) { if (slopes_len == 0) { return(is_valid=1); } if ((slopes_arr[0] - 51) * (slopes_arr[0] - 52) == 0) { return verify_slopes(slopes_arr+1, slopes_len-1); } return(is_valid=0); } func double_verify_slopes(first_arr : felt*, first_arr_len : felt, second_arr : felt*, second_arr_len : felt) -> (res : felt) { alloc_locals; let (local first_verify : felt) = verify_slopes(first_arr, first_arr_len); let (local second_verify : felt) = verify_slopes(second_arr, second_arr_len); if (first_verify+second_verify == 2) { return(res=1); } return (res=0); } // do not modify code on this line or above func is_silly_pyramid{range_check_ptr}(arr : felt*, len : felt) -> (n: felt, e : felt) { alloc_locals; let n_ : felt = is_n(arr, len); let e_ : felt = is_e(arr, len); return (n=n_,e=e_); } func is_n{range_check_ptr}(arr : felt*, len : felt)-> (n: felt){ if (len == 0){ return (n=1); } let left : felt = is_le(arr[0], -90); let in : felt = is_le(arr[0], -1); if (left == 1){ return (n=0); } if (in == 0){ return (n=0); } let (verify_res : felt) = is_n(arr+1, len-1); return (n=verify_res); } func is_e{range_check_ptr}(arr : felt*, len : felt)-> (e: felt){ if (len == 0){ return (e=1); } let left : felt = is_le(arr[0], 89); let in : felt = is_le(arr[0], 179); if (left == 1){ return (e=0); } if (in == 0){ return (e=0); } let (verify_res : felt) = is_e(arr+1, len-1); return (e=verify_res); } ## Publication Information - [Untitled](https://paragraph.com/@0x30784ddcd2b71947a35120ee67a0a9771cdaa07b/): Publication homepage - [All Posts](https://paragraph.com/@0x30784ddcd2b71947a35120ee67a0a9771cdaa07b/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0x30784ddcd2b71947a35120ee67a0a9771cdaa07b): Subscribe to updates