File size: 10,880 Bytes
bcffb9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
Evy is a new language. Bellow is an example of some evy code, and following that are some examples of evy programs
# Syntax by Example
The following examples will help you understand the syntax of Evy. For a
more formal definition of the syntax, see the
[Language Specification](spec.md). Built-in functions, such as `print`
and `circle`, are documented in the[Built-ins section](builtins.md).
## Comment
// This is a comment
## Declaration
x:num // declaration: num, string, bool, any, []num, {}string
y := 1 // declaration through type inference (num)
## Assignment
z = 5
## Expression
x := 5 * (y + z) - 2 / 7.6 // arithmetic number expression
b := !trace and debug or level == "" // bool expressions
## Strings
s1 := "quotation mark : \" " // escaping
s2 := "abc" + "π₯ͺ123" // concatenation
s3 := "newline: \n indentation: \t"
s4 := s2[0] // "a"
s5 := s2[1:5] // "bcπ₯ͺ1"
## `if` statements
if z > 0 and x != 0
print "block 1"
else if y != 0 or a == "abc"
print "block 2"
else
print "block 3"
end
### Nested `if`
if z > 0 and x != 0
if startswith str "a"
print "nested block 1"
else
print "nested block 2"
end
end
## Loop statements
### `while` loop
x := 0
while x < 10
print x
x = x + 1
end
### `for` β¦ `range` number
for x := range 5
print x // 0 1 2 3 4
end
for x := range 5 10
print x // 5 6 7 8 9
end
for x := range 1 10 2 // from to step
print x // 1 3 5 7 9
end
for x := range -10
print x // nothing. step is 1 by default.
end
### `for` β¦ `range` array
for x := range [1 2 3]
print x // 1 2 3
end
### `for` β¦ `range` map
m := { name:"Mali" sport:"climbing" }
for key := range m
print key m[key]
end
### `break`
x := 0
while true
print "tick... "
sleep 1
if x > 9
print "π₯"
break // `break` breaks out of the innermost loop
end
x = x + 1
end
## Function definition
func add:num a:num b:num
return a + b
end
### No return type
func foxprint s:string
print "π¦ " + s
end
### Variadic
func list args:any...
for arg := range args[:-1]
printf "%v, " arg
end
printf "%v" args[-1]
end
### Function calls
n := add 1 2 // 3
foxprint "πΎ" // π¦ πΎ
list 2 true "blue" // 2, true, blue
## Array
a1:[]num
a2:[][]string
a1 = [1 2 3 4] // type: num[]
a2 = [["1" "2"] ["a" "b"]] // type: string[][]
a3 := [true false] // type: bool[]
a4 := ["s1" // line break allowed
"s2"] // type: string[]
a5 := ["chars" 123] // type: any[]
a6:[]any // type: any[]
### Array element access
a1 := [1 2 3 4]
a2 := [["1" "2"] ["a" "b"]]
print a1[1] // 2
print a2[1][0] // "a"
print a1[-1] // 4
### Concatenation
a := [1 2 3 4]
a = a + [ 100 ] // [1 2 3 4 100]; optional extra whitespace
a = [0] + a + [101 102] // [0 1 2 3 4 100 101 102]
### Slicing
a := [1 2 3]
b := a[:2] // [1 2]
b = a[1:2] // [2]
b = a[-2:] // [2 3]
## Map
m1:{}any // keys used in literals or with `.` must be identifiers.
m1.name = "fox"
m1.age = 42
m1["key with space"] = "ππͺ"
m2 := {letters:"abc" name:"Jill"} // type: {}string
m3 := {} // type: {}any
m4 := {
letters:"abc" // line break allowed
nums:123
} // type: {}any
m5:{}[]num // map of array of numbers
m5.digits = [1 2 3]
m6:{}num
//m6.x = "y" // invalid, only num values allowed
### Map value access
m := {letters:"abc" name:"Jill"}
s := "letters"
print m.letters // abc
print m[s] // abc
print m["letters"] // abc
## `any`
x:any // any type, default value: false
m1:{}any // map with any value type
m2 := { letter:"a" number:1 }
arr1:[]any
arr2 := [ "b" 2 ]
## Type assertion
x:any
x = [ 1 2 3 4 ] // concrete type num[]
s := x.([]num)
## Type reflection
typeof "abc" // "string"
typeof true // "bool"
typeof [ 1 2 ] // "[]num"
typeof [[1 2] [3 4]] // "[][]num"
v:any
v = "π"
if (typeof v) == "string"
print "v is a string:" v
s := v.(string) // type assertion
print s+s // ππ
end
## Event handling
on key
print "key pressed"
end
Evy can only handle a limited set of events, such as key presses,
pointer movements, or periodic screen redraws.
### Event handlers with parameters
on key k:string
printf "%q pressed\n" k
end
# Example evy programs
```evy
// 1. Two Sum
// Solved
// Easy
// Topics
// Companies
// Hint
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// You can return the answer in any order.
// Example 1:
// Input: nums = [2,7,11,15], target = 9
// Output: [0,1]
// Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
// Example 2:
// Input: nums = [3,2,4], target = 6
// Output: [1,2]
// Example 3:
// Input: nums = [3,3], target = 6
// Output: [0,1]
// Constraints:
// 2 <= nums.length <= 104
// -109 <= nums[i] <= 109
// -109 <= target <= 109
// Only one valid answer exists.
// Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
func twosum:[]num nums:[]num target:num
m:{}num
for i := range (len nums)
v := nums[i]
if has m (sprintf "%v" (target - v))
return [m[sprintf "%v" (target - v)] i]
end
m[sprintf "%v" v] = i
end
return []
end
fails := 0
total := 0
func assert want:any got:any
total = total + 1
if want != got
fails = fails + 1
printf "want != got: want %v got %v\n" want got
end
end
func finished
printf "%v of %v tests passed\n" (total - fails) total
end
// -- Test Cases Start -- //
assert [0 1] (twosum [2 7 11 15] 9)
assert [1 2] (twosum [3 2 4] 6)
assert [0 1] (twosum [3 3] 6)
// -- Test Cases End -- //
finished
```
```
// 199. Binary Tree Right Side View
// Solved
// Medium
// Topics
// Companies
// Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
// Example 1:
// Input: root = [1,2,3,null,5,null,4]
// Output: [1,3,4]
// Example 2:
// Input: root = [1,null,3]
// Output: [1,3]
// Example 3:
// Input: root = []
// Output: []
// Constraints:
// The number of nodes in the tree is in the range [0, 100].
// -100 <= Node.val <= 100
func rightSideView:[]any treearr:[]any
root:any
root = buildBinaryTree treearr
queue := []
res := []
queue = queue + [root]
while (len queue) > 0
size := len queue
for i := range 0 size
node:{}any
node = queue[0].({}any)
queue = queue[1:]
if (has node "val") and i == size - 1
res = res + [node["val"]]
end
if (has node "left") and node["left"].({}any) != {}
queue = queue + [node["left"]]
end
if (has node "right") and node["right"].({}any) != {}
queue = queue + [node["right"]]
end
end
end
return res
end
fails := 0
total := 0
func assert want:any got:any
total = total + 1
if want != got
fails = fails + 1
printf "want != got: want %v got %v\n" want got
end
end
func finished
printf "%v of %v tests passed\n" (total - fails) total
end
func buildBinaryTree:{}any tree:[]any
root:{}any
rootany:any
rootany = root
queue := [rootany]
for i := range 0 (len tree)
if (len queue) == 0
break
end
node:{}any
node = queue[0].({}any)
queue = queue[1:]
anynull:any
anynull = "null"
if tree[i] != anynull
node["val"] = tree[i]
node["left"] = {}
node["right"] = {}
queue = queue + [node["left"]]
queue = queue + [node["right"]]
end
end
return root
end
// -- Test Cases Start -- //
assert [1 3 4 ""][:-1] (rightSideView [1 2 3 "null" 5 "null" 4])
assert [1 3 ""][:-1] (rightSideView [1 "null" 3])
assert [] (rightSideView [])
assert [1 3 4 ""][:-1] (rightSideView [1 2 3 4])
// // -- Test Cases End -- //
finished
```
```
// 412. Fizz Buzz
// Easy
// Topics
// Companies
// Given an integer n, return a string array answer (1-indexed) where:
// answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
// answer[i] == "Fizz" if i is divisible by 3.
// answer[i] == "Buzz" if i is divisible by 5.
// answer[i] == i (as a string) if none of the above conditions are true.
// Example 1:
// Input: n = 3
// Output: ["1","2","Fizz"]
// Example 2:
// Input: n = 5
// Output: ["1","2","Fizz","4","Buzz"]
// Example 3:
// Input: n = 15
// Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
// Constraints:
// 1 <= n <= 104
func fizzbuzz:[]string n:num
ans:[]string
for i := range 1 (n + 1)
s:string
if i % 3 == 0
s = s + "Fizz"
end
if i % 5 == 0
s = s + "Buzz"
end
if s == ""
s = sprintf "%v" i
end
ans = ans + [s]
end
return ans
end
fails := 0
total := 0
func assert want:any got:any
total = total + 1
if want != got
fails = fails + 1
printf "want != got: want %v got %v\n" want got
end
end
func finished
printf "%v of %v tests passed\n" (total - fails) total
end
// -- Test Cases Start -- //
assert ["1" "2" "Fizz"] (fizzbuzz 3)
assert ["1" "2" "Fizz" "4" "Buzz"] (fizzbuzz 5)
assert ["1" "2" "Fizz" "4" "Buzz" "Fizz" "7" "8" "Fizz" "Buzz" "11" "Fizz" "13" "14" "FizzBuzz"] (fizzbuzz 15)
// -- Test Cases End -- //
finished
```
With All of this, solve the following problem:
Write a function has_close_element that checks if in given list of numbers, are any two numbers closer to each other than given threshold.
Write the program in evy: |