Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Variable_declaration_reset

View file

@ -0,0 +1,10 @@
A decidely non-challenging task to highlight a potential difference between programming languages.
Using a straightforward longhand loop as in the JavaScript and Phix examples below, show the locations of elements which are identical to the immediately preceding element in {1,2,2,3,4,4,5}. The (non-blank) results may be 2,5 for zero-based or 3,6 if one-based. <br>
The purpose is to determine whether variable declaration (in block scope) resets the contents on every iteration.<br>
There is no particular judgement of right or wrong here, just a plain-speaking statement of subtle differences.<br>
Should your first attempt bomb with "unassigned variable" exceptions, feel free to code it as (say)
// int prev // crashes with unassigned variable
int prev = -1 // predictably no output
If your programming language does not support block scope (eg assembly) it should be omitted from this task.

View file

@ -0,0 +1,10 @@
BEGIN
[]INT s = ( 1, 2, 2, 3, 4, 4, 5 );
FOR i FROM LWB s TO UPB s DO
INT curr := s[ i ], prev;
IF IF i > LWB s THEN curr = prev ELSE FALSE FI THEN
print( ( i, newline ) )
FI;
prev := curr
OD
END

View file

@ -0,0 +1,12 @@
# syntax: GAWK -f VARIABLE_DECLARATION_RESET.AWK
BEGIN {
n = split("1,2,2,3,4,4,5",arr,",")
for (i=1; i<=n; i++) {
curr = arr[i]
if (i > 1 && prev == curr) {
printf("%s\n",i)
}
prev = curr
}
exit(0)
}

View file

@ -0,0 +1,25 @@
#include <array>
#include <iostream>
int main()
{
constexpr std::array s {1,2,2,3,4,4,5};
if(!s.empty())
{
int previousValue = s[0];
for(size_t i = 1; i < s.size(); ++i)
{
// in C++, variables in block scope are reset at each iteration
const int currentValue = s[i];
if(i > 0 && previousValue == currentValue)
{
std::cout << i << "\n";
}
previousValue = currentValue;
}
}
}

View file

@ -0,0 +1,27 @@
#include <stdio.h>
int main() {
int i, gprev = 0;
int s[7] = {1, 2, 2, 3, 4, 4, 5};
/* There is no output as 'prev' is created anew each time
around the loop and set explicitly to zero. */
for (i = 0; i < 7; ++i) {
// for (int i = 0, prev; i < 7; ++i) { // as below, see note
int curr = s[i];
int prev = 0;
// int prev; // produces same output as second loop
if (i > 0 && curr == prev) printf("%d\n", i);
prev = curr;
}
/* Now 'gprev' is used and reassigned
each time around the loop producing the desired output. */
for (i = 0; i < 7; ++i) {
int curr = s[i];
if (i > 0 && curr == gprev) printf("%d\n", i);
gprev = curr;
}
return 0;
}

View file

@ -0,0 +1,12 @@
// Variable declaration reset. Nigel Galloway: June 21st 2022
let s=[1;2;2;3;4;4;5]
// First let me write this in real F#, which rather avoids the whole issue
printfn "Real F#"
s|>List.pairwise|>List.iteri(fun i (n,g)->if n=g then printfn "%d" (i+1))
// Now let me take the opportunity to write some awful F# by translating the C++
printfn "C++ like awful F#"
let mutable previousValue = -1
for i in 0..s.Length-1 do
let currentValue=s.[i]
if previousValue = currentValue then printfn "%d" i
previousValue <- currentValue

View file

@ -0,0 +1,11 @@
USING: kernel math prettyprint sequences ;
[let
{ 1 2 2 3 4 4 5 } :> s
s length <iota> [| i |
i s nth -1 :> ( curr prev! )
i 0 > curr prev = and
[ i . ] when
curr prev!
] each
]

View file

@ -0,0 +1,11 @@
USING: kernel math prettyprint sequences ;
[let
{ 1 2 2 3 4 4 5 } -1 :> ( s prev! )
s length <iota> [| i |
i s nth :> curr
i 0 > curr prev = and
[ i . ] when
curr prev!
] each
]

View file

@ -0,0 +1,3 @@
USING: grouping math.vectors prettyprint sequences.extras ;
{ 1 2 2 3 4 4 5 } 2 <clumps> [ all-eq? ] arg-where 1 v+n .

View file

@ -0,0 +1,7 @@
Dim As Integer s(1 To 7) => {1,2,2,3,4,4,5}
For i As Integer = 1 To Ubound(s)
Dim As Integer curr = s(i), prev
If i > 1 And curr = prev Then Print i
prev = curr
Next i
Sleep

View file

@ -0,0 +1,29 @@
package main
import "fmt"
func main() {
s := []int{1, 2, 2, 3, 4, 4, 5}
// There is no output as 'prev' is created anew each time
// around the loop and set implicitly to zero.
for i := 0; i < len(s); i++ {
curr := s[i]
var prev int
if i > 0 && curr == prev {
fmt.Println(i)
}
prev = curr
}
// Now 'prev' is created only once and reassigned
// each time around the loop producing the desired output.
var prev int
for i := 0; i < len(s); i++ {
curr := s[i]
if i > 0 && curr == prev {
fmt.Println(i)
}
prev = curr
}
}

View file

@ -0,0 +1,2 @@
1+I.(}:=}.) 1 2 2 3 4 4 5
2 5

View file

@ -0,0 +1,15 @@
same2=: {{
i=. 0
r=. ,EMPTY
while. i < #y do.
curr=. i{y
if. i>0 do.
if. curr=prev do.
r=. r,i
end.
end.
prev=. curr
i=. i+1
end.
r
}}

View file

@ -0,0 +1,2 @@
same2 1,2,2,3,4,4,5
2 5

View file

@ -0,0 +1,16 @@
same3=: {{
i=. 0
r=. ,EMPTY
prev=. 99
while. i < #y do.
curr=. i{y
if. i>0 do.
if. curr=prev do.
r=. r,i
end.
end.
prev=. curr
i=. i+1
end.
r
}}

View file

@ -0,0 +1,25 @@
public class VariableDeclarationReset {
public static void main(String[] args) {
int[] s = {1, 2, 2, 3, 4, 4, 5};
// There is no output as 'prev' is created anew each time
// around the loop and set to zero.
for (int i = 0; i < s.length; ++i) {
int curr = s[i];
int prev = 0;
// int prev; // triggers "error: variable prev might not have been initialized"
if (i > 0 && curr == prev) System.out.println(i);
prev = curr;
}
int gprev = 0;
// Now 'gprev' is used and reassigned
// each time around the loop producing the desired output.
for (int i = 0; i < s.length; ++i) {
int curr = s[i];
if (i > 0 && curr == gprev) System.out.println(i);
gprev = curr;
}
}
}

View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>variable declaration reset</title>
</head>
<body>
<script>
"use strict";
let s = [1, 2, 2, 3, 4, 4, 5];
for (let i=0; i<7; i+=1) {
let curr = s[i], prev;
if (i>0 && (curr===prev)) {
console.log(i);
}
prev = curr;
}
</script>
</body>
</html>

View file

@ -0,0 +1,4 @@
[1,2,2,3,4,4,5]
| . as $array
| range(1;length)
| select( $array[.] == $array[.-1])

View file

@ -0,0 +1,7 @@
s = [1, 2, 2, 3, 4, 4, 5]
for i in eachindex(s)
curr = s[i]
i > 1 && curr == prev && println(i)
prev = curr
end

View file

@ -0,0 +1,9 @@
s = [1, 2, 2, 3, 4, 4, 5]
prev = -1
for i in eachindex(s)
global prev
curr = s[i]
i > 1 && curr == prev && println(i)
prev = curr
end

View file

@ -0,0 +1,5 @@
s = [1, 2, 2, 3, 4, 4, 5]
for i in eachindex(s)[begin+1:end] # or 2:length(s)
s[i] == s[i - 1] && println(i)
end

View file

@ -0,0 +1,2 @@
&=/'2':1 2 2 3 4 4 5
1 4

View file

@ -0,0 +1,6 @@
let s = [1, 2, 2, 3, 4, 4, 5]
for i in 0..s.high:
let curr = s[i]
if i > 0 and curr == prev:
echo i
var prev = curr

View file

@ -0,0 +1,7 @@
let s = [1, 2, 2, 3, 4, 4, 5]
for i in 0..s.high:
let curr = s[i]
var prev: int
if i > 0 and curr == prev:
echo i
prev = curr

View file

@ -0,0 +1,7 @@
let s = [1, 2, 2, 3, 4, 4, 5]
var prev: int
for i in 0..s.high:
let curr = s[i]
if i > 0 and curr == prev:
echo i
prev = curr

View file

@ -0,0 +1,34 @@
100H:
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5;END;
/* CONSOLE OUTPUT ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$STRING( .( 0DH, 0AH, '$' ) ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE INITIAL( '.....$' ), W BYTE;
N$STR( W := LAST( N$STR ) - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
DECLARE S( 6 ) BYTE INITIAL( 1, 2, 2, 3, 4, 4, 5 );
DECLARE I BYTE;
DO I = 0 TO LAST( S );
DO;
DECLARE ( CURR, PREV ) BYTE;
CURR = S( I );
IF I > 1 AND CURR = PREV THEN DO;
CALL PR$NUMBER( I );
CALL PR$NL;
END;
PREV = CURR;
END;
END;
EOF

View file

@ -0,0 +1,6 @@
@s = <1 2 2 3 4 4 5>;
for ($i = 0; $i < 7; $i++) {
$curr = $s[$i];
if ($i > 1 and $curr == $prev) { print "$i\n" }
$prev = $curr;
}

View file

@ -0,0 +1,11 @@
use strict;
use warnings;
use feature 'state';
my @s = <1 2 2 3 4 4 5>;
for (my $i = 0; $i < 7; $i++) {
my $curr = $s[$i];
state $prev;
if ($i > 1 and $curr == $prev) { print "$i\n" }
$prev = $curr;
}

View file

@ -0,0 +1,11 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">prev</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--

View file

@ -0,0 +1,7 @@
s = [1, 2, 2, 3, 4, 4, 5]
for i in range(len(s)):
curr = s[i]
if i > 0 and curr == prev:
print(i)
prev = curr

View file

@ -0,0 +1,9 @@
my @s = 1, 2, 2, 3, 4, 4, 5;
loop (my $i = 0; $i < 7; $i += 1) {
my $curr = @s[$i];
my $prev;
if $i > 1 and $curr == $prev {
say $i;
}
$prev = $curr;
}

View file

@ -0,0 +1,9 @@
my @s = 1, 2, 2, 3, 4, 4, 5;
quietly loop (my $i = 0; $i < 7; $i += 1) {
my $curr = @s[$i];
my $prev;
if $i > 1 and $curr == $prev {
say $i;
}
$prev = $curr;
}

View file

@ -0,0 +1,9 @@
my @s = 1, 2, 2, 3, 4, 4, 5;
loop (my $i = 0; $i < 7; $i += 1) {
my $curr = @s[$i];
our $prev;
if $i > 1 and $curr == $prev {
say $i;
}
$prev = $curr;
}

View file

@ -0,0 +1,9 @@
my @s = 1, 2, 2, 3, 4, 4, 5;
loop (my $i = 0; $i < 7; $i += 1) {
my $curr = @s[$i];
state $prev;
if $i > 1 and $curr == $prev {
say $i;
}
$prev = $curr;
}

View file

@ -0,0 +1,9 @@
no strict;
@s = 1, 2, 2, 3, 4, 4, 5;
loop ($i = 0; $i < 7; $i += 1) {
$curr = @s[$i];
if $i > 1 and $curr == $prev {
say $i;
}
$prev = $curr;
}

View file

@ -0,0 +1,9 @@
Red[]
s: [1 2 2 3 4 4 5]
repeat i length? s [
curr: s/:i
if all [i > 1 curr = prev][
print i
]
prev: curr
]

View file

@ -0,0 +1,17 @@
$ include "seed7_05.s7i";
const proc: main is func
local
const array integer: s is [] (1, 2, 2, 3, 4, 4, 5);
var integer: i is 0;
var integer: curr is 0;
var integer: prev is 0;
begin
for i range 1 to length(s) do
curr := s[i];
if i > 1 and curr = prev then
writeln(i);
end if;
prev := curr;
end for;
end func;

View file

@ -0,0 +1,25 @@
fn main() {
s := [1, 2, 2, 3, 4, 4, 5]
// There is no output as 'prev' is created anew each time
// around the loop and set implicitly to zero.
for i := 0; i < s.len; i++ {
curr := s[i]
mut prev := 0
if i > 0 && curr == prev {
println(i)
}
prev = curr
}
// Now 'prev' is created only once and reassigned
// each time around the loop producing the desired output.
mut prev := 0
for i := 0; i < s.len; i++ {
curr := s[i]
if i > 0 && curr == prev {
println(i)
}
prev = curr
}
}

View file

@ -0,0 +1,20 @@
Option Strict On
Option Explicit On
Imports System.IO
Module vMain
Public Sub Main
Dim s As Integer() = New Integer(){1, 2, 2, 3, 4, 4, 5}
For i As Integer = 0 To Ubound(s)
Dim curr As Integer = s(i)
Dim prev As Integer
If i > 1 AndAlso curr = prev Then
Console.Out.WriteLine(i)
End If
prev = curr
Next i
End Sub
End Module

View file

@ -0,0 +1,19 @@
var s = [1, 2, 2, 3, 4, 4, 5]
// There is no output as 'prev' is created anew each time
// around the loop and set implicitly to null.
for (i in 0...s.count) {
var curr = s[i]
var prev
if (i > 0 && curr == prev) System.print(i)
prev = curr
}
// Now 'prev' is created only once and reassigned
// each time around the loop producing the desired output.
var prev
for (i in 0...s.count) {
var curr = s[i]
if (i > 0 && curr == prev) System.print(i)
prev = curr
}