7
\$\begingroup\$

Description

In this challenge, you need to write a function that takes in a list of strings and returns the smallest string based on their ASCII value. You should assume that the input list contains only lowercase alphabetical strings.

Test Cases

["a", "b", "c"] => "a"
["hello", "world", "code"] => "code"
["aaa", "aa", ""] => ""
["cat", "bat", "rat"] => "bat"
["zab", "xyz", "abcd"] => "zab"

Write the function in as few bytes as possible. The code itself can be a function, process or anything provided that it gives the desired output. Use of other ASCII characters like big or small letters, with space function must be supported.

asked Mar 3, 2023 at 16:13
\$\endgroup\$
11
  • 2
    \$\begingroup\$ "Smallest string based on ASCII value" as in the string with the smallest sum of ASCII values? \$\endgroup\$ Commented Mar 3, 2023 at 16:27
  • \$\begingroup\$ @Seggan precisely correct, the individual ASCII values are added \$\endgroup\$ Commented Mar 3, 2023 at 16:28
  • 5
    \$\begingroup\$ I suggest you clarify that in the post \$\endgroup\$ Commented Mar 3, 2023 at 16:31
  • \$\begingroup\$ Suggested test case: ["zab", "xyz", "abcd"] => "zab" \$\endgroup\$ Commented Mar 3, 2023 at 17:52
  • 1
    \$\begingroup\$ I suggest you remove the requirement of supporting extended ASCII. With standard ASCII the logic of the algorithm is probably the same, and it allows more languages to participate \$\endgroup\$ Commented Mar 3, 2023 at 18:03

29 Answers 29

3
\$\begingroup\$

05AB1E, 5 bytes

ΣÇO}н

Try it online!

Explanation

ΣÇO}н # Implicit input
Σ } # Sort by:
 Ç # ASCII values
 O # Sum
 н # First item
 # Implicit output
answered Mar 3, 2023 at 16:35
\$\endgroup\$
3
\$\begingroup\$

Factor, 22 bytes

[ [ sum ] infimum-by ]

Try it online!

answered Mar 3, 2023 at 18:13
\$\endgroup\$
3
\$\begingroup\$

Java (JDK), 57 bytes

l->l.stream().min((a,b)->a.chars().sum()-b.chars().sum())

Try it online!

Takes input as a List<String> and returns an Optional<String>.

answered Mar 3, 2023 at 18:15
\$\endgroup\$
3
\$\begingroup\$

BQN, (削除) 18 (削除ここまで) 16 bytes

-2 from Dominic van Essen for replacing '0' with @.

{x⊑ ̃⊑⍒(+ ́@⊸-) ̈x}

Try it online!

Explanation

{x⊑ ̃⊑⍒(+ ́@⊸-) ̈x} 
{ ̈x} 1. For each string in the (monadic) input x
{ ( @⊸-) } 2a. convert the string to (negative) its unicode representation...
{ (+ ́ ) } 2b. and perform a sum reduction.
{ ⍒( ) } 3. return descending-sorted indices.
{ ⊑ } 4. take the first.
{x⊑ ̃ } 5. take this index into the input x where ̃ "flips" the next function.
answered Mar 3, 2023 at 17:24
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Save 2 bytes by changing '0' to @... \$\endgroup\$ Commented Mar 18, 2023 at 9:41
  • \$\begingroup\$ Cheers, I didn't know about @ :s \$\endgroup\$ Commented Mar 19, 2023 at 16:47
2
\$\begingroup\$

Vyxal, (削除) 5 (削除ここまで) 4 bytes

‡C∑P

Try it Online!

-1 thanks to @mathcat

Explanation

‡C∑P # Implicit input
‡ P # Minimum by:
 C # ASCII values
 ∑ # Sum
 # Implicit output

Old:

C∑)ṡh # Implicit input
 )ṡ # Sort by:
C # ASCII values
 ∑ # Sum
 h # First item
 # Implicit output 
answered Mar 3, 2023 at 16:41
\$\endgroup\$
2
  • \$\begingroup\$ 4 bytes \$\endgroup\$ Commented Mar 3, 2023 at 17:17
  • \$\begingroup\$ @mathcat thanks, updated \$\endgroup\$ Commented Mar 3, 2023 at 17:32
2
\$\begingroup\$

C (gcc), 92 bytes

f(s,t,u,i,c)char**s,*t,*u;{for(i=~0,u=0;t=*s;c<i+0U?u=*s,i=c:0,s++)for(c=0;*t;c+=*t++);s=u;}

Try it online!

Ungolfed:

f(s,t,u,i,c)char**s,*t,*u;{
 for(i=~0, // initialize minimum string sum to largest possible
 u=0; // initialize smallest string (in case of empty list)
 t=*s; // scan list
 c<i+0U?u=*s,i=c:0, // reset minimum string sum and pointer if smaller
 s++)
 for(c=0;*t;c+=*t++); // get string sum
 s=u; // return smallest string
}
answered Mar 3, 2023 at 18:11
\$\endgroup\$
2
\$\begingroup\$

Raku, 17 bytes

*.min(*.ords.sum)

Try it online!

*.ords.sum is an anonymous function that calculates the sum of the ordinal values of its string argument. That function is passed as the argument to *.min, which returns the element of the input list with the smallest value computed by the first function.

answered Mar 18, 2023 at 14:17
\$\endgroup\$
2
  • \$\begingroup\$ Your explanation seems to break off at a \$\endgroup\$ Commented Mar 18, 2023 at 14:50
  • \$\begingroup\$ Huh! Fixed. Dunno how that happened. \$\endgroup\$ Commented Mar 18, 2023 at 17:38
2
\$\begingroup\$

Ruby, 19 bytes

->x{x.min_by &:sum}

Try it online!

answered May 7, 2024 at 13:29
\$\endgroup\$
1
\$\begingroup\$

Swift, (削除) 64 (削除ここまで) 69 bytes

Now with fewer compiler crashes!

{0ドル.map{(0ドル.reduce(0){0ドル+Int(1ドル.asciiValue!)},0ドル)}.min{0ドル.0<1ドル.0}!.1}

SwiftFiddle link

answered Mar 3, 2023 at 16:45
\$\endgroup\$
1
  • \$\begingroup\$ The original version only worked for short strings; after more than a couple characters, it would overflow the limit for UInt8. However, when you hardcode the input, constant-folding causes this overflow to happen at compile-time, an the compiler crashes. \$\endgroup\$ Commented Mar 3, 2023 at 16:49
1
\$\begingroup\$

Python, 44 bytes

lambda l:min(l,key=lambda x:sum(map(ord,x)))

Attempt This Online!

Commented

lambda l: # Anonymous function taking l, a list of strings
 min(l, ) # Return the minimum of l...
 key= # ...using the following function:
 lambda x: # Function taking x, a string
 sum( ) # Sum the following:
 map(ord,x) # Convert all characters to ASCII values
answered Mar 3, 2023 at 16:45
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 59 bytes

a=>a.sort(g=(x,y)=>x>=x&&~~eval(Buffer(x).join`+`)-g(y))[0]

Try it online!

Commented

a => // a[] = input array
a.sort(g = // g is a recursive callback function
 (x, y) => // for each pair (x, y) to be tested:
 x >= x && // stop if x is undefined
 ~~eval( // otherwise, evaluate as JS code:
 Buffer(x) // the ASCII codes of x
 .join`+` // joined with '+'
 ) // end of eval()
 - g(y) // subtract the result of a recursive call with x = y
) // end of sort()
[0] // keep the first entry
answered Mar 3, 2023 at 17:11
\$\endgroup\$
1
\$\begingroup\$

Japt -g, 4 bytes

Takes input as an array of character arrays.

ñÈxc

Try it

ñÈxc :Implicit input of 2D array
ñ :Sort by
 È :Function
 x : Sum of
 c : Codepoints
 :Implicit output of first element
answered Mar 3, 2023 at 18:35
\$\endgroup\$
1
\$\begingroup\$

Excel, 78 bytes

=LET(x,A1#,y,ROW(A:A),@SORTBY(x,MMULT(TOROW(y^0),IFERROR(CODE(MID(x,y,1)),))))

where A1# is a horizontal spilled range containing the strings.

answered Mar 3, 2023 at 21:02
\$\endgroup\$
1
  • \$\begingroup\$ In theory I thought =@SORTBY(A1#,MAP(A1#,LAMBDA(x,SUM(IFERROR(CODE(MID(x,ROW(A:A),1)),)+1)))) for 73 bytes would work. Using the implicit intersection operator should return the top-left cell. However, it's either bugged or not working as I expected. \$\endgroup\$ Commented Mar 6, 2023 at 14:37
1
\$\begingroup\$

Zsh +old utils, 47 (削除) 55 (削除ここまで) (削除) 103 (削除ここまで) bytes

Using the filesystem and the old System V sum* command:

for i;echo $i>$[++n];set `sum -s *|sort -n`;<3ドル

Try it online! (削除) 55 bytes (削除ここまで)

* On MacOS, cksum -o2 is equivalent to GNU sum -s. According to Wikipedia, the BSD checksum algorithm is "useless from a security perspective" 😛


103 bytes, using arrays and builtins only:

for x;{w=;for a (${(s::)${x:?}})((w+=#a));k+=($w)}
for i ({1..$#})((k[i]==$(printf ${(n)k})))&&<<<$@[i]

Try it online!
Explanation of code is in earlier revisions of this answer

answered Mar 3, 2023 at 19:36
\$\endgroup\$
0
1
\$\begingroup\$

Burlesque, 9 bytes

{)**++}<m

Try it online!

{
 )** # Map to ord
 ++ # sum
}<m # Minimum by
answered Mar 19, 2023 at 12:25
\$\endgroup\$
1
\$\begingroup\$

Jelly, 5 bytes

OS$ÞḢ

Try it online!

Explanation

OS$ÞḢ # Main link
 $ # Last two links as a monad:
O # Ordinals
 S # Sum
 Þ # Sort the input by this monad
 Ḣ # First item (minimum)
answered Mar 18, 2023 at 11:59
\$\endgroup\$
1
\$\begingroup\$

Stax, (削除) 9 (削除ここまで) 5 bytes

And pure ASCII!

{|+}e

Run and debug it (with test cases)

Explanation

{ }e # minimum by
 |+ # sum
answered Mar 9, 2023 at 20:24
\$\endgroup\$
1
\$\begingroup\$

janet, 51 bytes

(fn[l](((sort-by first(map(fn[x][(sum x)x])l))0)1))

in janet, you can save bytes by not using spaces between brackets, which i personally think is very ugly for a lisp-like language, so here's an de-uglified version and an example

(fn [l] (((sort-by first (map (fn [x] [(sum x) x]) l)) 0) 1))
$ janet
Janet 1.29.1-20230709 openbsd/x64/clang - '(doc)' for help
repl:1:> (def F (fn[l](((sort-by first(map(fn[x][(sum x)x])l))0)1)))
<function 0x028B13B5F580>
repl:2:> (F ["zab" "xyz" "abcd"])
"zab"
answered Aug 27, 2023 at 13:00
\$\endgroup\$
1
\$\begingroup\$

Pip, (削除) 12 (削除ここまで) 10 bytes

@Y$+A*_SKg

Attempt This Online!

-2 thanks to @DLosc

Explanation

@Y$+A*_SKg ; Input on command line
 g ; Input list
 SK ; Sorted by:
 $+ ; Sum of
 A* ; ASCII values of
 _ ; The string
@Y ; First item (minimum)
 ; Implicit output
answered Mar 18, 2023 at 13:28
\$\endgroup\$
1
  • \$\begingroup\$ Just noticing this answer a year later... you can save two bytes on the function by using a lambda expression $+A*_. \$\endgroup\$ Commented Mar 6, 2024 at 7:29
1
\$\begingroup\$

Perl 5 + -pl, 49 bytes

($_)=sort{unpack($x="%32A*",$a)-unpack$x,$b}$_,<>

Try it online!

Explanation

sorts a list of the current input ($_) and the remaining lines of input (<>) based on the value of unpacking the string using "exotic templates", having the first index of the result re-assigned to $_ to be implicitly -printed.

answered May 7, 2024 at 12:17
\$\endgroup\$
1
\$\begingroup\$

R, (削除) 50 (削除ここまで) 47 bytes

  • -3 bytes by pajonk
which.min(Map(\(x)sum(utf8ToInt(x)),scan(,"")))

Attempt This Online!

This outputs a named vector with the smallest value string as the name and the value is the string's position in the input list. If this is against the rules, then the following code for 7 bytes more will provide the clean output: names(which.min(Map(\(x)sum(utf8ToInt(x)),scan(,""))))

answered May 7, 2024 at 8:20
\$\endgroup\$
1
  • 1
    \$\begingroup\$ -3 bytes using Map (apparently which.min works also for lists...) \$\endgroup\$ Commented May 7, 2024 at 17:59
0
\$\begingroup\$

Arturo, 42 bytes

$=>[minimum&=>[∑to[:integer]to[:char]&]]

Try it

answered Mar 3, 2023 at 18:39
\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 74 bytes

.
$&$&96$*~
0T1`l`L`\w\w
{`[J-Z]
9$&
}T`_L`dL
\d
$*~
+`(\w)~
~1ドル
O`
1G`
~

Try it online! Only accepts strings of lowercase letters. Explanation:

.
$&$&96$*~

Duplicate each letter and suffix 96 ~s to each pair.

0T1`l`L`\w\w

Uppercase alternate letters.

{`[J-Z]
9$&
}T`_L`dL

Map uppercase letters into digits that sum to their index i.e. A=1 to Z=998.

\d
$*~

Expand the digits into that many ~s.

+`(\w)~
~1ドル

Move all of the ~s to the beginning of the string.

O`

Sort the strings into order. Since ~ sorts after z, this means that the string with the fewest ~s will sort first.

1G`

Keep only the first string.

~

Delete the ~s.

answered Mar 3, 2023 at 22:55
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 15 bytes

UMθ⟦↨Eιc/oλ1ι⟧⊟⌊θ

Try it online! Link is to verbose version of code. Explanation:

UMθ⟦↨Eιc/oλ1ι⟧

Calculate the sum of ASCII values for each string.

⊟⌊θ

Output the string with the smallest sum.

answered Mar 4, 2023 at 0:04
\$\endgroup\$
0
\$\begingroup\$

PowerShell Core, 55 bytes

($args|Sort-Object{"$($_|% t*y|%{'+',+$_[0]})"|iex})[0]

Try it online!

answered Mar 10, 2023 at 1:44
\$\endgroup\$
0
\$\begingroup\$

Pyth, 6 bytes

.msCMb

Try it online!

answered Mar 18, 2023 at 2:03
\$\endgroup\$
1
0
\$\begingroup\$

Husk, 2 bytes

◄Σ

Try it online!

◄ # minlon: Element that minimizes function result
 Σ # chrsum: Sum of code points
answered Mar 18, 2023 at 8:48
\$\endgroup\$
0
\$\begingroup\$

BQN, (削除) 15 (削除ここまで) 14 bytes

⊑∘⍋∘(+ ́ ̈-⟜@)⊑⊢

Try it at BQN REPL

⊑∘⍋∘(+ ́ ̈-⟜@)⊑⊢
 ( ̈ ) # for each element of input
 -⟜@ # subtract the null character from each letter
 # (thereby getting codepoint values)
 + ́ # and sum by fold-addition
⊑∘ # now get the first element of 
 ⍋∘ # the indices of sorted elements
 ⊑ # and use this to select from
 ⊢ # the elements of the input
answered Mar 18, 2023 at 9:40
\$\endgroup\$
0
\$\begingroup\$

Scala 3, 34 bytes

 _.minByOption(_.map(_.toInt).sum)

Attempt This Online!

answered May 7, 2024 at 13:05
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.