Exactly three adjacent 3 in lists: Difference between revisions
(add FreeBASIC) |
(Added Wren) |
||
Line 94: | Line 94: | ||
5 true |
5 true |
||
done... |
done... |
||
</pre> |
|||
=={{header|Wren}}== |
|||
<lang ecmascript>var lists = [ |
|||
[9,3,3,3,2,1,7,8,5], |
|||
[5,2,9,3,3,7,8,4,1], |
|||
[1,4,3,6,7,3,8,3,2], |
|||
[1,2,3,4,5,6,7,8,9], |
|||
[4,6,8,7,2,3,3,3,1] |
|||
] |
|||
System.print("Exactly three adjacent 3's:") |
|||
for (list in lists) { |
|||
var threesCount = list.count { |n| n == 3 } |
|||
var condition = false |
|||
if (threesCount == 3) { |
|||
var i = list.indexOf(3) |
|||
condition = list[i+1] == 3 && list[i+2] == 3 |
|||
} |
|||
System.print("%(list) -> %(condition)") |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Exactly three adjacent 3's: |
|||
[9, 3, 3, 3, 2, 1, 7, 8, 5] -> true |
|||
[5, 2, 9, 3, 3, 7, 8, 4, 1] -> false |
|||
[1, 4, 3, 6, 7, 3, 8, 3, 2] -> false |
|||
[1, 2, 3, 4, 5, 6, 7, 8, 9] -> false |
|||
[4, 6, 8, 7, 2, 3, 3, 3, 1] -> true |
|||
</pre> |
</pre> |
Revision as of 08:28, 7 December 2021
- Task
Let given 5 lists of ints:
list[1] = [9,3,3,3,2,1,7,8,5]
list[2] = [5,2,9,3,3,7,8,4,1]
list[3] = [1,4,3,6,7,3,8,3,2]
list[4] = [1,2,3,4,5,6,7,8,9]
list[5] = [4,6,8,7,2,3,3,3,1]
Print 'true' if the value '3' appears in the list exactly 3 times and they are adjacent ones otherwise print 'false'.
FreeBASIC
<lang freebasic>dim as integer list(1 to 5, 1 to 9) = {_
{9,3,3,3,2,1,7,8,5}, {5,2,9,3,3,7,8,4,1},_ {1,4,3,6,7,3,8,3,2}, {1,2,3,4,5,6,7,8,9},_ {4,6,8,7,2,3,3,3,1}}
dim as boolean go, pass dim as integer i, j, c
for i = 1 to 5
go = false pass = true c = 0 for j = 1 to 9 if list(i, j) = 3 then c+=1 go = true else if go = true and c<>3 then pass=false go = false end if next j print i;" "; if c = 3 and pass then print true else print false
next i</lang>
- Output:
1 true 2 false 3 false 4 false 5 true
Ring
<lang ring> see "working..." + nl
list = List(5) list[1] = [9,3,3,3,2,1,7,8,5] list[2] = [5,2,9,3,3,7,8,4,1] list[3] = [1,4,3,6,7,3,8,3,2] list[4] = [1,2,3,4,5,6,7,8,9] list[5] = [4,6,8,7,2,3,3,3,1]
for n = 1 to 5
strn = list2str(list[n]) cnt = count(strn,"3") if cnt = 3 for m = 1 to len(list[n])-2 if list[n][m] = 3 and list[n][m+1] = 3 and list[n][m+2] = 3 flag = 1 exit ok next else flag = 0 ok if flag = 1 see "" + n + " true" + nl else see "" + n + " false" + nl ok
next
see "done..." + nl
func count(cString,dString)
sum = 0 while substr(cString,dString) > 0 sum++ cString = substr(cString,substr(cString,dString)+len(string(sum))) end return sum
</lang>
- Output:
working... 1 true 2 false 3 false 4 false 5 true done...
Wren
<lang ecmascript>var lists = [
[9,3,3,3,2,1,7,8,5], [5,2,9,3,3,7,8,4,1], [1,4,3,6,7,3,8,3,2], [1,2,3,4,5,6,7,8,9], [4,6,8,7,2,3,3,3,1]
] System.print("Exactly three adjacent 3's:") for (list in lists) {
var threesCount = list.count { |n| n == 3 } var condition = false if (threesCount == 3) { var i = list.indexOf(3) condition = list[i+1] == 3 && list[i+2] == 3 } System.print("%(list) -> %(condition)")
}</lang>
- Output:
Exactly three adjacent 3's: [9, 3, 3, 3, 2, 1, 7, 8, 5] -> true [5, 2, 9, 3, 3, 7, 8, 4, 1] -> false [1, 4, 3, 6, 7, 3, 8, 3, 2] -> false [1, 2, 3, 4, 5, 6, 7, 8, 9] -> false [4, 6, 8, 7, 2, 3, 3, 3, 1] -> true