Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

take account of q/258761/#comment571102_258850
Source Link
Kai Burghardt
  • 1.2k
  • 2
  • 11
  • 15

Pascal, 590(削除) 590 (削除ここまで) 358 bytes

  • the implementation-dependent behavior of the selection order in a for ... in set member iteration statement must be ascending,
  • the implementation-defined default TotalWidth for integer-type write arguments must be sufficiently large in order to leave some space between a character and the number, and
  • the implementation-defined set of available characters contains both upper and lowercase characters.

The GPC (GNU Pascal Compiler ) fulfills these conditions on an x86‐64 Linux system if compiling with the ‐‐extended-pascal option.

program p(input,output);var c:char;f:array[char]of integer value[otherwise 0];begin while not EOF do begin read(c);case c of'a':c:='A';'b':c:='B';'c':c:='C';'d':c:='D';'e':c:='E';'f':c:='F';'g':c:='G';'h':c:='H';'i':c:='I';'j':c:='J';'k':c:='K';'l':c:='L';'m':c:='M';'n':c:='N';'o':c:='O';'p':c:='P';'q':c:='Q';'r':c:='R';'s':c:='S';'t':c:='T';'u':c:='U';'v':c:='V';'w':c:='W';'x':c:='X';'y':c:='Y';'z';c:=(c+'ABCDEFGHIJKLMNOPQRSTUVWXYZ')[index('abcdefghijklmnopqrstuvwxyz',c:='Z';otherwise end;f[c])+1];f[c]:=f[c]+1 end;for c in['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']do if 0<f[c]then writeLn(c,f[c])end.
program letterFrequency(input, output);
 var
 c: char;
 letterFrequency: array[char] of integer value [otherwise 0];
 begin
 while not EOF(input) do
 begin
 { `read(input, c)` is equivalent to `c := input^; get(input)`. }
 read(input, c);
 
 { Pascal does not guarantee that the characters 'a' through 'z'
  There areis arrangedno built-in contiguous order. }
 case c of
 'a': c := 'A';
 'b': c := 'B';
 'c': c := 'C';
 'd': c := 'D';
 'e': c := 'E';
 'f': c := 'F';
 'g': c := 'G';
 'h': c := 'H';
 'i': c := 'I';
 'j': c := 'J';
  'k': c := 'K';
 'l': c := 'L';
 'm': c := 'M';
 "change case" function 'n':as cPascal :=does 'N';not
 'o': c := 'O';
 guarantee the coexistence of lower and 'p':uppercase cletters. :=The 'P';EP
 'q': c := 'Q';
  function `index(source, pattern)` returns the 'r':index cof :=the 'R';first
 's': c := 'S';
 occurence of `pattern` in `source`. If 't':`pattern` cis :=not 'T';found,
 'u': c := 'U';
 zero is returned. Remember, `string` 'v':indices care :=1-based, 'V';thus
 'w': c := 'W';
 add `1` to select the prepended 'x':value cof :=`c`. 'X';}
 'y': c := 'Y';
  'z': (c := 'Z';
 + otherwise'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
 { There needs to be an empty `otherwise` branch[index('abcdefghijklmnopqrstuvwxyz', otherwise [no
 pun intended] an error may arise. The GNU Pascal Compiler shows
  "`case' selector value matches no case constant" for example. }
 c) + end;1];
 
 letterFrequency[c] := letterFrequency[c] + 1;
 end;
 
 for c in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] do
 begin
 if letterFrequency[c] > 0 then
 begin
 writeLn(c, letterFrequency[c]);
 end;
 end;
 end.

See also FreePascal implementation .

Pascal, 590 bytes

  • the implementation-dependent behavior of the selection order in a for ... in set member iteration statement must be ascending,
  • the implementation-defined TotalWidth for integer-type write arguments must be sufficiently large in order to leave some space between a character and the number, and
  • the implementation-defined set of available characters contains both upper and lowercase characters.
program p(input,output);var c:char;f:array[char]of integer value[otherwise 0];begin while not EOF do begin read(c);case c of'a':c:='A';'b':c:='B';'c':c:='C';'d':c:='D';'e':c:='E';'f':c:='F';'g':c:='G';'h':c:='H';'i':c:='I';'j':c:='J';'k':c:='K';'l':c:='L';'m':c:='M';'n':c:='N';'o':c:='O';'p':c:='P';'q':c:='Q';'r':c:='R';'s':c:='S';'t':c:='T';'u':c:='U';'v':c:='V';'w':c:='W';'x':c:='X';'y':c:='Y';'z':c:='Z';otherwise end;f[c]:=f[c]+1 end;for c in['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']do if 0<f[c]then writeLn(c,f[c])end.
program letterFrequency(input, output);
 var
 c: char;
 letterFrequency: array[char] of integer value [otherwise 0];
 begin
 while not EOF(input) do
 begin
 { `read(input, c)` is equivalent to `c := input^; get(input)`. }
 read(input, c);
 
 { Pascal does not guarantee that the characters 'a' through 'z'
  are arranged in contiguous order. }
 case c of
 'a': c := 'A';
 'b': c := 'B';
 'c': c := 'C';
 'd': c := 'D';
 'e': c := 'E';
 'f': c := 'F';
 'g': c := 'G';
 'h': c := 'H';
 'i': c := 'I';
 'j': c := 'J';
  'k': c := 'K';
 'l': c := 'L';
 'm': c := 'M';
  'n': c := 'N';
 'o': c := 'O';
 'p': c := 'P';
 'q': c := 'Q';
  'r': c := 'R';
 's': c := 'S';
  't': c := 'T';
 'u': c := 'U';
 'v': c := 'V';
 'w': c := 'W';
  'x': c := 'X';
 'y': c := 'Y';
  'z': c := 'Z';
  otherwise
 { There needs to be an empty `otherwise` branch, otherwise [no
 pun intended] an error may arise. The GNU Pascal Compiler shows
  "`case' selector value matches no case constant" for example. }
  end;
 
 letterFrequency[c] := letterFrequency[c] + 1;
 end;
 
 for c in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] do
 begin
 if letterFrequency[c] > 0 then
 begin
 writeLn(c, letterFrequency[c]);
 end;
 end;
 end.

Pascal, (削除) 590 (削除ここまで) 358 bytes

  • the implementation-dependent behavior of the selection order in a for ... in set member iteration statement must be ascending,
  • the implementation-defined default TotalWidth for integer-type write arguments must be sufficiently large in order to leave some space between a character and the number, and
  • the implementation-defined set of available characters contains both upper and lowercase characters.

The GPC (GNU Pascal Compiler ) fulfills these conditions on an x86‐64 Linux system if compiling with the ‐‐extended-pascal option.

program p(input,output);var c:char;f:array[char]of integer value[otherwise 0];begin while not EOF do begin read(c);c:=(c+'ABCDEFGHIJKLMNOPQRSTUVWXYZ')[index('abcdefghijklmnopqrstuvwxyz',c)+1];f[c]:=f[c]+1 end;for c in['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']do if 0<f[c]then writeLn(c,f[c])end.
program letterFrequency(input, output);
 var
 c: char;
 letterFrequency: array[char] of integer value [otherwise 0];
 begin
 while not EOF(input) do
 begin
 { `read(input, c)` is equivalent to `c := input^; get(input)`. }
 read(input, c);
 
 { There is no built-in "change case" function as Pascal does not
 guarantee the coexistence of lower and uppercase letters. The EP
 function `index(source, pattern)` returns the index of the first
 occurence of `pattern` in `source`. If `pattern` is not found,
 zero is returned. Remember, `string` indices are 1-based, thus
 add `1` to select the prepended value of `c`. }
 c := (c + 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
 [index('abcdefghijklmnopqrstuvwxyz', c) + 1];
 
 letterFrequency[c] := letterFrequency[c] + 1;
 end;
 
 for c in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] do
 begin
 if letterFrequency[c] > 0 then
 begin
 writeLn(c, letterFrequency[c]);
 end;
 end;
 end.

See also FreePascal implementation .

Source Link
Kai Burghardt
  • 1.2k
  • 2
  • 11
  • 15

Pascal, 590 bytes

This full program requires a processor supporting features of ISO standard 10206 "Extended Pascal". Furthermore,

  • the implementation-dependent behavior of the selection order in a for ... in set member iteration statement must be ascending,
  • the implementation-defined TotalWidth for integer-type write arguments must be sufficiently large in order to leave some space between a character and the number, and
  • the implementation-defined set of available characters contains both upper and lowercase characters.
program p(input,output);var c:char;f:array[char]of integer value[otherwise 0];begin while not EOF do begin read(c);case c of'a':c:='A';'b':c:='B';'c':c:='C';'d':c:='D';'e':c:='E';'f':c:='F';'g':c:='G';'h':c:='H';'i':c:='I';'j':c:='J';'k':c:='K';'l':c:='L';'m':c:='M';'n':c:='N';'o':c:='O';'p':c:='P';'q':c:='Q';'r':c:='R';'s':c:='S';'t':c:='T';'u':c:='U';'v':c:='V';'w':c:='W';'x':c:='X';'y':c:='Y';'z':c:='Z';otherwise end;f[c]:=f[c]+1 end;for c in['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']do if 0<f[c]then writeLn(c,f[c])end.

Ungolfed:

program letterFrequency(input, output);
 var
 c: char;
 letterFrequency: array[char] of integer value [otherwise 0];
 begin
 while not EOF(input) do
 begin
 { `read(input, c)` is equivalent to `c := input^; get(input)`. }
 read(input, c);
 
 { Pascal does not guarantee that the characters 'a' through 'z'
 are arranged in contiguous order. }
 case c of
 'a': c := 'A';
 'b': c := 'B';
 'c': c := 'C';
 'd': c := 'D';
 'e': c := 'E';
 'f': c := 'F';
 'g': c := 'G';
 'h': c := 'H';
 'i': c := 'I';
 'j': c := 'J';
 'k': c := 'K';
 'l': c := 'L';
 'm': c := 'M';
 'n': c := 'N';
 'o': c := 'O';
 'p': c := 'P';
 'q': c := 'Q';
 'r': c := 'R';
 's': c := 'S';
 't': c := 'T';
 'u': c := 'U';
 'v': c := 'V';
 'w': c := 'W';
 'x': c := 'X';
 'y': c := 'Y';
 'z': c := 'Z';
 otherwise
 { There needs to be an empty `otherwise` branch, otherwise [no
 pun intended] an error may arise. The GNU Pascal Compiler shows
 "`case' selector value matches no case constant" for example. }
 end;
 
 letterFrequency[c] := letterFrequency[c] + 1;
 end;
 
 for c in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] do
 begin
 if letterFrequency[c] > 0 then
 begin
 writeLn(c, letterFrequency[c]);
 end;
 end;
 end.

AltStyle によって変換されたページ (->オリジナル) /