Maximum Hamming distance among a list of padded strings
Clash Royale CLAN TAG#URR8PPP
up vote
18
down vote
favorite
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. If the strings are not of equal length, the Hamming distance is not defined.
Challenge
Write a program or function that finds the largest Hamming distance from among all pairs of strings from a list of strings, padded as required according to the rules described below.
The characters will be from within a-zA-Z0-9
.
The strings may not be equal in length, so for each comparison the shorter string has to be padded as follows:
- wrap the string from the beginning as many times as needed to match the required length
- change the cases of the letters each odd time wrapping (1st, 3rd, 5th, etc.)
- leave things outside
a-zA-Z
unchanged when wrapping
For example, let's say you need to pad the 5 character string ab9Cd
so that it ends up with 18 characters. You would end up with:
ab9CdAB9cDab9CdAB9
^^^^^ ^^^
with ^
added underneath the 1st and 3rd wraps to highlight to case changes.
Input/Output
Input/output format is flexible. You can assume the input has at least two strings, and that all strings will have at least one character.
The output is an integer.
Rules
This is code-golf. Standard rules apply.
Test cases
[ "a", "b" ] => 1
[ "a", "b", "c" ] => 1
[ "a", "a", "c" ] => 1
[ "abc", "abcd" ] => 1
[ "abc12D5", "abC34d3", "ABC14dabc23DAbC89d"] => 17
[ "a", "Aaa", "AaaA", "aAaAa", "aaaaaaaaaaaaaa", "AAaAA", "aAa" ] => 8
["AacaAc", "Aab"] => 2
Reference implementation
I tested the examples with (completely ungolfed) R code that you can try here to compare any other examples you might try out with your code.
code-golf string
add a comment |Â
up vote
18
down vote
favorite
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. If the strings are not of equal length, the Hamming distance is not defined.
Challenge
Write a program or function that finds the largest Hamming distance from among all pairs of strings from a list of strings, padded as required according to the rules described below.
The characters will be from within a-zA-Z0-9
.
The strings may not be equal in length, so for each comparison the shorter string has to be padded as follows:
- wrap the string from the beginning as many times as needed to match the required length
- change the cases of the letters each odd time wrapping (1st, 3rd, 5th, etc.)
- leave things outside
a-zA-Z
unchanged when wrapping
For example, let's say you need to pad the 5 character string ab9Cd
so that it ends up with 18 characters. You would end up with:
ab9CdAB9cDab9CdAB9
^^^^^ ^^^
with ^
added underneath the 1st and 3rd wraps to highlight to case changes.
Input/Output
Input/output format is flexible. You can assume the input has at least two strings, and that all strings will have at least one character.
The output is an integer.
Rules
This is code-golf. Standard rules apply.
Test cases
[ "a", "b" ] => 1
[ "a", "b", "c" ] => 1
[ "a", "a", "c" ] => 1
[ "abc", "abcd" ] => 1
[ "abc12D5", "abC34d3", "ABC14dabc23DAbC89d"] => 17
[ "a", "Aaa", "AaaA", "aAaAa", "aaaaaaaaaaaaaa", "AAaAA", "aAa" ] => 8
["AacaAc", "Aab"] => 2
Reference implementation
I tested the examples with (completely ungolfed) R code that you can try here to compare any other examples you might try out with your code.
code-golf string
1
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
Suggested test case:["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.
â Mr. Xcoder
Aug 13 at 21:57
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46
add a comment |Â
up vote
18
down vote
favorite
up vote
18
down vote
favorite
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. If the strings are not of equal length, the Hamming distance is not defined.
Challenge
Write a program or function that finds the largest Hamming distance from among all pairs of strings from a list of strings, padded as required according to the rules described below.
The characters will be from within a-zA-Z0-9
.
The strings may not be equal in length, so for each comparison the shorter string has to be padded as follows:
- wrap the string from the beginning as many times as needed to match the required length
- change the cases of the letters each odd time wrapping (1st, 3rd, 5th, etc.)
- leave things outside
a-zA-Z
unchanged when wrapping
For example, let's say you need to pad the 5 character string ab9Cd
so that it ends up with 18 characters. You would end up with:
ab9CdAB9cDab9CdAB9
^^^^^ ^^^
with ^
added underneath the 1st and 3rd wraps to highlight to case changes.
Input/Output
Input/output format is flexible. You can assume the input has at least two strings, and that all strings will have at least one character.
The output is an integer.
Rules
This is code-golf. Standard rules apply.
Test cases
[ "a", "b" ] => 1
[ "a", "b", "c" ] => 1
[ "a", "a", "c" ] => 1
[ "abc", "abcd" ] => 1
[ "abc12D5", "abC34d3", "ABC14dabc23DAbC89d"] => 17
[ "a", "Aaa", "AaaA", "aAaAa", "aaaaaaaaaaaaaa", "AAaAA", "aAa" ] => 8
["AacaAc", "Aab"] => 2
Reference implementation
I tested the examples with (completely ungolfed) R code that you can try here to compare any other examples you might try out with your code.
code-golf string
The Hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different. If the strings are not of equal length, the Hamming distance is not defined.
Challenge
Write a program or function that finds the largest Hamming distance from among all pairs of strings from a list of strings, padded as required according to the rules described below.
The characters will be from within a-zA-Z0-9
.
The strings may not be equal in length, so for each comparison the shorter string has to be padded as follows:
- wrap the string from the beginning as many times as needed to match the required length
- change the cases of the letters each odd time wrapping (1st, 3rd, 5th, etc.)
- leave things outside
a-zA-Z
unchanged when wrapping
For example, let's say you need to pad the 5 character string ab9Cd
so that it ends up with 18 characters. You would end up with:
ab9CdAB9cDab9CdAB9
^^^^^ ^^^
with ^
added underneath the 1st and 3rd wraps to highlight to case changes.
Input/Output
Input/output format is flexible. You can assume the input has at least two strings, and that all strings will have at least one character.
The output is an integer.
Rules
This is code-golf. Standard rules apply.
Test cases
[ "a", "b" ] => 1
[ "a", "b", "c" ] => 1
[ "a", "a", "c" ] => 1
[ "abc", "abcd" ] => 1
[ "abc12D5", "abC34d3", "ABC14dabc23DAbC89d"] => 17
[ "a", "Aaa", "AaaA", "aAaAa", "aaaaaaaaaaaaaa", "AAaAA", "aAa" ] => 8
["AacaAc", "Aab"] => 2
Reference implementation
I tested the examples with (completely ungolfed) R code that you can try here to compare any other examples you might try out with your code.
code-golf string
edited Aug 14 at 15:09
asked Aug 13 at 19:38
ngm
2,07920
2,07920
1
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
Suggested test case:["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.
â Mr. Xcoder
Aug 13 at 21:57
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46
add a comment |Â
1
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
Suggested test case:["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.
â Mr. Xcoder
Aug 13 at 21:57
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46
1
1
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
Suggested test case:
["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.â Mr. Xcoder
Aug 13 at 21:57
Suggested test case:
["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.â Mr. Xcoder
Aug 13 at 21:57
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46
add a comment |Â
9 Answers
9
active
oldest
votes
up vote
7
down vote
Jelly, 20 bytes
Not really happy with it. Should be golfable, even to ~15 bytes perhaps.
LÃÂÃ
Âcõá¹Â/sḢL$Ã
ÂsÃÂeFn)çá¹Â
Try it online!
or Check out a test suite!
Explanation
LÃÂà Âcõá¹Â/sḢL$à ÂsÃÂeFn)çṠFull program or monadic link. N = input. | Example: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÃÂ Sort N by length. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']] (in Jelly, strings are list of characters)
à Âc Unordered pairs: [x, y] for all distinct x, y in N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']]]
Here, by distinct, I mean at different positions. |
õ ) Map with a monadic link. |
á¹Â/ Mold x like y. That is, cycle x until it reaches length y. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
sḢL$ Split into chunks of x's length. | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3']]]
à ÂsÃÂe Swap the case of even-indexed chunks (1-indexed). | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'c', '3', '4', 'D', '3'], ['a', 'b', 'C', '3']]]
F Flatten. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'A', 'B', 'C', '1', '2', 'd', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
n Vectorized inequality with y. | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]]]
ç After ending the map, sum each bool (0 or 1) array. | [[5], [17], [14]]
á¹ Maximum. | 17
I'm totally not familiar with Jelly, but I think you can omitLÃ
and still get the same maximum at the end.
â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...
â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try["AacaAc", "Aab"]
.
â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
add a comment |Â
up vote
5
down vote
Python 2, 86 bytes
lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)
Try it online!
Given two strings, s,t
, zip((s+s.swapcase())*len(t),t))
will be a list of tuples of length len(t)
since zip
truncates to the shortest iterable. If len(s)<len(t)
, then this "pads out" s
with the desired case swapping and we calculate the sum
of differing characters.
If len(t)<=len(s)
, then the resulting sum
will be less than or equal to the sum
if we were evaluating t,s
; so it has no effect on the resulting max
in that case.
You can usey!=
instead of!=y
to save 1 byte
â Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 111 bytes
a=>a.map(m=S=>a.map(s=>B(S).map((c,k)=>m=(c^(c=B(s)[k%(l=s.length)])^(k/l&c>9)<<5&&++x)<m?m:x,x=0)),B=Buffer)|m
Try it online!
add a comment |Â
up vote
3
down vote
Jelly, 19 bytes
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
LÃÂÃ
Âcç/â¬á¹Â
Try it online!
LÃÂÃ
Âcç/â¬á¹Â
LÃÂ Sort by length
Ã
Âc unordered pairs
⬠to each of the pairs
ç/ find the hamming distance with molding and swapping case (helper link)
á¹ maximum
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
W wrap the shorter string
á¹ repeat this string once for each character in the second string
ÃÂe for every other repeated string
Ã
Âs swap case
F flatten
= characterwise equality check between the two strings. If the first
string is longer, the leftover characters are appended to the result.
e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
ÃÂ0 count the number of 0s, giving the Hamming distance.
add a comment |Â
up vote
2
down vote
Ruby, 89 82 bytes
Creates the cross-product of the input list against itself before calculating the Hamming distance of each pair, using a duplication method similar to Chas Brown's answer. Ruby can't zip strings together or add booleans without additional overhead, though, so it becomes necessary to iterate through the pair of strings manually instead.
-7 bytes from GB.
->aa.product(a).maps,t.max
Try it online!
1
82 bytes
â G B
Aug 14 at 11:27
add a comment |Â
up vote
2
down vote
Java 10, 748 740 667 666 616 bytes
This has to be the most dense and unreadable, yet the longest golf I ever came up with.
Call method h(String)
with an explicit array (no var args): eg,
h(new String "a", "b", "c");
returns 1
.
char e(boolean w,char c)96<c&c<123)?c^32:c);String p(String s,int l)var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;int d(String s,String t)int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;int h(String s,String t)int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);int h(Strings)int l=s.length,i=0,j;intn=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();
You can Try It Online!
Ungolfed and commented:
// Encode the character (swap case)
char e(boolean w, char c) 96 < c & c < 123) ? c ^ 32 : c);
// Pad the string to desired length
String p(String s, int l)
var p = "";
int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
var w = 1 < 0;
for (; i < n; ++i, w = !w)
for (char c : s.toCharArray())
p += e(w, c);
for (; j < r;)
p += e(w, s.charAt(j++));
return p;
// Calculate the actual hamming distance between two same-length strings
int d(String s, String t)
int l = s.length(), n = 0, i = 0;
for (; i < l;)
if (s.charAt(i) != t.charAt(i++))
++n;
return n;
// Pad the strings as needed and return their hamming distance
int h(String s, String t)
int l = s.length(), m = t.length();
return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
// Dispatch the strings and gather their hamming distances, return the max
int h(String s)
int l = s.length, i = 0, j;
int n = new int[l * l];
for (; i < l; ++i)
for (j = i; ++j < l;)
n[i * l + j] = h(s[i], s[j]);
return java.util.Arrays.stream(n).max().getAsInt();
I know a better solution can be achieved, especially for the string pairing part.
EDIT: shave off 8 bytes by changing the size of the int array in hammingDistance()
to the square of the numbe of strings given. It also fixes an ArrayIndexOutOfBounds
thrown in one of the test cases.
EDIT 2: Saved 33 bytes thanks to Kevin Cruijssen's comments: class declaration removed, names shortened to 1 char, operators changed, etc.
EDIT 3: Save 1 byte and reach Satan-approved score by changing method with var-arg to array.
EDIT 4: Save another 50 bytes thanks to Kevin Cruijssen, again: update Java version from 8 to 10 to use var
keyword, removed StringBuilder
instance, etc.
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead ofhammingDistance
used
or some other unused variable. Most of your&&
can be&
and||
can be|
.c^' '
can bec^32
.boolean w = false;
can beboolean w=0>1;
.i=0
in the loop initialization can be removed and change the,i,j
to,i=0,j
.++j
can be removed and++
can be added to the.charAt(j++)
..toString()
can be+""
.for(j=i+1;j<l;++j)
can befor(j=0;++j<l;)
. Etc. etc.
â Kevin Cruijssen
Aug 14 at 13:38
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
Upvoted for the Satan-approved score. xD Some more small things:StringBuilder
can beStringBuffer
(if you switch to Java 10 it could bevar b=new StringBuffer(l);
. Theboolean
andchar
can then also bevar
. If you don't have Java 10 locally, it is available on TIO). In addition,for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can befor(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove theStringBuffer
completely and just useString
and+=
instead ofappend
.
â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
add a comment |Â
up vote
1
down vote
05AB1E, 33 29 bytes
ÃÂü)â¬ééõÃÂDÃ
¡Ã«s`gâÂÂ}îâŠâÂÂøâ¬ö≬ÃÂ_OÃÂ
Try it online or verify all test cases.
Can most likely be halved in byte-count, but it works..
Explanation:
ÃÂ # Enclose the input-list (adding the first item to the end of the list)
# i.e. ['ABC1','abcD','abCd32e'] â ['ABC1','abcD','abCd32e','ABC1']
ü) # Pair-vectorize each of them
# i.e. ['ABC1','abcD','abCd32e','ABC1']
# â [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
⎠# Sort each pair by length
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
é # Store this list in the register to re-use later on
õ } # Map each inner list in this list to:
ÃÂ # Head extracted
# i.e. ['abcD','abCd32e'] â 'abcD' and ['abCd32e']
DÃ
¡ # Duplicate it, and swap the capitalization of the copy
# i.e. 'abcD' â 'ABCd'
ë # Then merge it together
# i.e. 'abcD' and 'ABCd' â 'abcDABCd'
s` # Swap so the tail-list is at the top of the stack, and get it's single item
# i.e. ['abCd32e'] â 'abCd32e'
g # Get the length of that
# i.e. 'abCd32e' â 7
â # Extend/shorten the string to that length
# i.e. 'abcDABCd' and 7 â 'abcDABC'
î # Get the saved list from the register again
⊠# Get the tail from each
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â ['abcD','abCd32e','abCd32e']
â # Pair it with the other list
# i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
# â [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
ø # Zip it, swapping rows / columns
# i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
# â [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
â¬ö # And then zip each pair again
# i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
# â [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
⬠# Then for each inner list
⬠# And for each inner string
ÃÂ # Check if all characters are the same
# i.e. 'aa' â 1
# i.e. 'cC' â 0
_ # And inverse the booleans
# i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
# â [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O # Then sum each inner list
# i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] â [4,5,6]
ÃÂ # And take the max as result
# i.e. [4,5,6] â 6
add a comment |Â
up vote
1
down vote
Java 11, 387 bytes
a->int l=a.length,i=l,t,j=0,C=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p)var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91for(int c:C)j=c>j?c:j;return j;
Try it online. (NOTE: Since Java 11 isn't on TIO yet, String.repeat(int)
has been emulated as repeat(String,int)
for the same byte-count.)
Explanation:
a-> // Method with String-array parameter and integer return-type
int l=a.length, // Length of the input-array
i=l, // Index-integer, starting at the length
t,j=0, // Temp-integers
C=new int[l]; // Count-array the same size as the input
var p=new String[l][2]; // String-pairs array the same size as the input
for(;i-->0 // Loop `i` in the range [`l`, 0)
; // After every iteration:
p[i][0]= // Set the first String of the pair at index `i` to:
a[t>0?i:j],// The smallest of the `i`'th or `j`'th Strings of the input-array
p[i][1]= // And set the second String of the pair at index `i` to:
a[t>0?j:i])// The largest of the `i`'th or `j`'th Strings of the input-array
t=a[i].length()< // If the length of the `i`'th item is smaller than
a[j=-~i%l].length()?// the length of the `i+1`'th item
// (and set `j` to this `i+1` with wrap-around to 0 for the last item
1 // Set `t` to 1 as flag
: // Else:
0; // Set `t` to 0 as flag
// We've now created the String pairs, where each pair is sorted by length
i=0; // Reset `i` to 0
for(var P:p) // Loop over the pairs
var s=""; // Temp-String starting empty
for(var x:P[0].getBytes())
// Loop over the characters of the first String of the pair
s+= // Append the temp-String with:
(char)(x>64&x<91 // After every iteration of the outer loop,
// increase `i` by 1 for the next iteration
for(int c:C) // Now loop over the calculated counts
j=c>j?c:j; // And set `j` to the maximum
return j; // And finally return this maximum `j` as result
add a comment |Â
up vote
1
down vote
R, 173 bytes
function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))
Try it online!
@ngm : I tried my best to golf your code (with my heavy customizations of course) but, as you well know, R is not very golfy in manipulating strings :P
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might includeouter
to get all the combinations, and doing modular arithmetic on the code points in lieu ofchartr
.
â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
add a comment |Â
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
Jelly, 20 bytes
Not really happy with it. Should be golfable, even to ~15 bytes perhaps.
LÃÂÃ
Âcõá¹Â/sḢL$Ã
ÂsÃÂeFn)çá¹Â
Try it online!
or Check out a test suite!
Explanation
LÃÂà Âcõá¹Â/sḢL$à ÂsÃÂeFn)çṠFull program or monadic link. N = input. | Example: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÃÂ Sort N by length. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']] (in Jelly, strings are list of characters)
à Âc Unordered pairs: [x, y] for all distinct x, y in N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']]]
Here, by distinct, I mean at different positions. |
õ ) Map with a monadic link. |
á¹Â/ Mold x like y. That is, cycle x until it reaches length y. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
sḢL$ Split into chunks of x's length. | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3']]]
à ÂsÃÂe Swap the case of even-indexed chunks (1-indexed). | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'c', '3', '4', 'D', '3'], ['a', 'b', 'C', '3']]]
F Flatten. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'A', 'B', 'C', '1', '2', 'd', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
n Vectorized inequality with y. | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]]]
ç After ending the map, sum each bool (0 or 1) array. | [[5], [17], [14]]
á¹ Maximum. | 17
I'm totally not familiar with Jelly, but I think you can omitLÃ
and still get the same maximum at the end.
â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...
â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try["AacaAc", "Aab"]
.
â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
add a comment |Â
up vote
7
down vote
Jelly, 20 bytes
Not really happy with it. Should be golfable, even to ~15 bytes perhaps.
LÃÂÃ
Âcõá¹Â/sḢL$Ã
ÂsÃÂeFn)çá¹Â
Try it online!
or Check out a test suite!
Explanation
LÃÂà Âcõá¹Â/sḢL$à ÂsÃÂeFn)çṠFull program or monadic link. N = input. | Example: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÃÂ Sort N by length. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']] (in Jelly, strings are list of characters)
à Âc Unordered pairs: [x, y] for all distinct x, y in N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']]]
Here, by distinct, I mean at different positions. |
õ ) Map with a monadic link. |
á¹Â/ Mold x like y. That is, cycle x until it reaches length y. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
sḢL$ Split into chunks of x's length. | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3']]]
à ÂsÃÂe Swap the case of even-indexed chunks (1-indexed). | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'c', '3', '4', 'D', '3'], ['a', 'b', 'C', '3']]]
F Flatten. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'A', 'B', 'C', '1', '2', 'd', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
n Vectorized inequality with y. | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]]]
ç After ending the map, sum each bool (0 or 1) array. | [[5], [17], [14]]
á¹ Maximum. | 17
I'm totally not familiar with Jelly, but I think you can omitLÃ
and still get the same maximum at the end.
â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...
â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try["AacaAc", "Aab"]
.
â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Jelly, 20 bytes
Not really happy with it. Should be golfable, even to ~15 bytes perhaps.
LÃÂÃ
Âcõá¹Â/sḢL$Ã
ÂsÃÂeFn)çá¹Â
Try it online!
or Check out a test suite!
Explanation
LÃÂà Âcõá¹Â/sḢL$à ÂsÃÂeFn)çṠFull program or monadic link. N = input. | Example: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÃÂ Sort N by length. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']] (in Jelly, strings are list of characters)
à Âc Unordered pairs: [x, y] for all distinct x, y in N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']]]
Here, by distinct, I mean at different positions. |
õ ) Map with a monadic link. |
á¹Â/ Mold x like y. That is, cycle x until it reaches length y. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
sḢL$ Split into chunks of x's length. | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3']]]
à ÂsÃÂe Swap the case of even-indexed chunks (1-indexed). | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'c', '3', '4', 'D', '3'], ['a', 'b', 'C', '3']]]
F Flatten. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'A', 'B', 'C', '1', '2', 'd', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
n Vectorized inequality with y. | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]]]
ç After ending the map, sum each bool (0 or 1) array. | [[5], [17], [14]]
á¹ Maximum. | 17
Jelly, 20 bytes
Not really happy with it. Should be golfable, even to ~15 bytes perhaps.
LÃÂÃ
Âcõá¹Â/sḢL$Ã
ÂsÃÂeFn)çá¹Â
Try it online!
or Check out a test suite!
Explanation
LÃÂà Âcõá¹Â/sḢL$à ÂsÃÂeFn)çṠFull program or monadic link. N = input. | Example: ["abc12D5", "abC34d3", "ABC14dabc23DAbC89d"]
LÃÂ Sort N by length. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']] (in Jelly, strings are list of characters)
à Âc Unordered pairs: [x, y] for all distinct x, y in N. | [[['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'C', '3', '4', 'd', '3']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'C', '1', '4', 'd', 'a', 'b', 'c', '2', '3', 'D', 'A', 'b', 'C', '8', '9', 'd']]]
Here, by distinct, I mean at different positions. |
õ ) Map with a monadic link. |
á¹Â/ Mold x like y. That is, cycle x until it reaches length y. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1', '2', 'D', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3', '4', 'd', '3', 'a', 'b', 'C', '3']]
sḢL$ Split into chunks of x's length. | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3', '4', 'd', '3'], ['a', 'b', 'C', '3']]]
à ÂsÃÂe Swap the case of even-indexed chunks (1-indexed). | [[['a', 'b', 'c', '1', '2', 'D', '5']], [['a', 'b', 'c', '1', '2', 'D', '5'], ['A', 'B', 'C', '1', '2', 'd', '5'], ['a', 'b', 'c', '1']], [['a', 'b', 'C', '3', '4', 'd', '3'], ['A', 'B', 'c', '3', '4', 'D', '3'], ['a', 'b', 'C', '3']]]
F Flatten. | [['a', 'b', 'c', '1', '2', 'D', '5'], ['a', 'b', 'c', '1', '2', 'D', '5', 'A', 'B', 'C', '1', '2', 'd', '5', 'a', 'b', 'c', '1'], ['a', 'b', 'C', '3', '4', 'd', '3', 'A', 'B', 'c', '3', '4', 'D', '3', 'a', 'b', 'C', '3']]
n Vectorized inequality with y. | [[[0, 0, 1, 1, 1, 1, 1]], [[1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1]]]
ç After ending the map, sum each bool (0 or 1) array. | [[5], [17], [14]]
á¹ Maximum. | 17
edited Aug 13 at 20:32
answered Aug 13 at 20:03
Mr. Xcoder
30.2k757193
30.2k757193
I'm totally not familiar with Jelly, but I think you can omitLÃ
and still get the same maximum at the end.
â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...
â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try["AacaAc", "Aab"]
.
â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
add a comment |Â
I'm totally not familiar with Jelly, but I think you can omitLÃ
and still get the same maximum at the end.
â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...
â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try["AacaAc", "Aab"]
.
â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
I'm totally not familiar with Jelly, but I think you can omit
LÃ
and still get the same maximum at the end.â Chas Brown
Aug 13 at 21:41
I'm totally not familiar with Jelly, but I think you can omit
LÃ
and still get the same maximum at the end.â Chas Brown
Aug 13 at 21:41
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,
á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown Ugh, no, I should need that. Otherwise, instead of padding the shortest to the length of the longest one,
á¹Â/
would instead trim the longest to the length of the shortest one in some cases, which is not what we want.... I guess the test cases are too well chosen (and this is a rather unfortunate coincidence)...â Mr. Xcoder
Aug 13 at 21:48
@ChasBrown As an example, try
["AacaAc", "Aab"]
.â Mr. Xcoder
Aug 13 at 21:57
@ChasBrown As an example, try
["AacaAc", "Aab"]
.â Mr. Xcoder
Aug 13 at 21:57
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
Ah yes, I see... I need to learn me some more Jelly... :)
â Chas Brown
Aug 13 at 22:02
add a comment |Â
up vote
5
down vote
Python 2, 86 bytes
lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)
Try it online!
Given two strings, s,t
, zip((s+s.swapcase())*len(t),t))
will be a list of tuples of length len(t)
since zip
truncates to the shortest iterable. If len(s)<len(t)
, then this "pads out" s
with the desired case swapping and we calculate the sum
of differing characters.
If len(t)<=len(s)
, then the resulting sum
will be less than or equal to the sum
if we were evaluating t,s
; so it has no effect on the resulting max
in that case.
You can usey!=
instead of!=y
to save 1 byte
â Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
add a comment |Â
up vote
5
down vote
Python 2, 86 bytes
lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)
Try it online!
Given two strings, s,t
, zip((s+s.swapcase())*len(t),t))
will be a list of tuples of length len(t)
since zip
truncates to the shortest iterable. If len(s)<len(t)
, then this "pads out" s
with the desired case swapping and we calculate the sum
of differing characters.
If len(t)<=len(s)
, then the resulting sum
will be less than or equal to the sum
if we were evaluating t,s
; so it has no effect on the resulting max
in that case.
You can usey!=
instead of!=y
to save 1 byte
â Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Python 2, 86 bytes
lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)
Try it online!
Given two strings, s,t
, zip((s+s.swapcase())*len(t),t))
will be a list of tuples of length len(t)
since zip
truncates to the shortest iterable. If len(s)<len(t)
, then this "pads out" s
with the desired case swapping and we calculate the sum
of differing characters.
If len(t)<=len(s)
, then the resulting sum
will be less than or equal to the sum
if we were evaluating t,s
; so it has no effect on the resulting max
in that case.
Python 2, 86 bytes
lambda a:max(sum(x!=y for x,y in zip((s+s.swapcase())*len(t),t))for s in a for t in a)
Try it online!
Given two strings, s,t
, zip((s+s.swapcase())*len(t),t))
will be a list of tuples of length len(t)
since zip
truncates to the shortest iterable. If len(s)<len(t)
, then this "pads out" s
with the desired case swapping and we calculate the sum
of differing characters.
If len(t)<=len(s)
, then the resulting sum
will be less than or equal to the sum
if we were evaluating t,s
; so it has no effect on the resulting max
in that case.
edited Aug 13 at 21:07
answered Aug 13 at 20:38
Chas Brown
4,1361319
4,1361319
You can usey!=
instead of!=y
to save 1 byte
â Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
add a comment |Â
You can usey!=
instead of!=y
to save 1 byte
â Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
You can use
y!=
instead of !=y
to save 1 byteâ Mr. Xcoder
Aug 13 at 20:43
You can use
y!=
instead of !=y
to save 1 byteâ Mr. Xcoder
Aug 13 at 20:43
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
@Mr. Xcoder: Thx, but I ended up drastically reworking my solution...
â Chas Brown
Aug 13 at 21:10
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 111 bytes
a=>a.map(m=S=>a.map(s=>B(S).map((c,k)=>m=(c^(c=B(s)[k%(l=s.length)])^(k/l&c>9)<<5&&++x)<m?m:x,x=0)),B=Buffer)|m
Try it online!
add a comment |Â
up vote
3
down vote
JavaScript (Node.js), 111 bytes
a=>a.map(m=S=>a.map(s=>B(S).map((c,k)=>m=(c^(c=B(s)[k%(l=s.length)])^(k/l&c>9)<<5&&++x)<m?m:x,x=0)),B=Buffer)|m
Try it online!
add a comment |Â
up vote
3
down vote
up vote
3
down vote
JavaScript (Node.js), 111 bytes
a=>a.map(m=S=>a.map(s=>B(S).map((c,k)=>m=(c^(c=B(s)[k%(l=s.length)])^(k/l&c>9)<<5&&++x)<m?m:x,x=0)),B=Buffer)|m
Try it online!
JavaScript (Node.js), 111 bytes
a=>a.map(m=S=>a.map(s=>B(S).map((c,k)=>m=(c^(c=B(s)[k%(l=s.length)])^(k/l&c>9)<<5&&++x)<m?m:x,x=0)),B=Buffer)|m
Try it online!
answered Aug 13 at 23:24
Arnauld
63.3k580267
63.3k580267
add a comment |Â
add a comment |Â
up vote
3
down vote
Jelly, 19 bytes
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
LÃÂÃ
Âcç/â¬á¹Â
Try it online!
LÃÂÃ
Âcç/â¬á¹Â
LÃÂ Sort by length
Ã
Âc unordered pairs
⬠to each of the pairs
ç/ find the hamming distance with molding and swapping case (helper link)
á¹ maximum
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
W wrap the shorter string
á¹ repeat this string once for each character in the second string
ÃÂe for every other repeated string
Ã
Âs swap case
F flatten
= characterwise equality check between the two strings. If the first
string is longer, the leftover characters are appended to the result.
e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
ÃÂ0 count the number of 0s, giving the Hamming distance.
add a comment |Â
up vote
3
down vote
Jelly, 19 bytes
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
LÃÂÃ
Âcç/â¬á¹Â
Try it online!
LÃÂÃ
Âcç/â¬á¹Â
LÃÂ Sort by length
Ã
Âc unordered pairs
⬠to each of the pairs
ç/ find the hamming distance with molding and swapping case (helper link)
á¹ maximum
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
W wrap the shorter string
á¹ repeat this string once for each character in the second string
ÃÂe for every other repeated string
Ã
Âs swap case
F flatten
= characterwise equality check between the two strings. If the first
string is longer, the leftover characters are appended to the result.
e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
ÃÂ0 count the number of 0s, giving the Hamming distance.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Jelly, 19 bytes
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
LÃÂÃ
Âcç/â¬á¹Â
Try it online!
LÃÂÃ
Âcç/â¬á¹Â
LÃÂ Sort by length
Ã
Âc unordered pairs
⬠to each of the pairs
ç/ find the hamming distance with molding and swapping case (helper link)
á¹ maximum
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
W wrap the shorter string
á¹ repeat this string once for each character in the second string
ÃÂe for every other repeated string
Ã
Âs swap case
F flatten
= characterwise equality check between the two strings. If the first
string is longer, the leftover characters are appended to the result.
e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
ÃÂ0 count the number of 0s, giving the Hamming distance.
Jelly, 19 bytes
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
LÃÂÃ
Âcç/â¬á¹Â
Try it online!
LÃÂÃ
Âcç/â¬á¹Â
LÃÂ Sort by length
Ã
Âc unordered pairs
⬠to each of the pairs
ç/ find the hamming distance with molding and swapping case (helper link)
á¹ maximum
Wá¹ÂÃ
ÂsÃÂeF=ÃÂ0
W wrap the shorter string
á¹ repeat this string once for each character in the second string
ÃÂe for every other repeated string
Ã
Âs swap case
F flatten
= characterwise equality check between the two strings. If the first
string is longer, the leftover characters are appended to the result.
e.g. 'abABab' and 'xbA' give [0,1,1,'B','a','b']
ÃÂ0 count the number of 0s, giving the Hamming distance.
edited Aug 14 at 17:48
answered Aug 14 at 17:41
dylnan
3,4982526
3,4982526
add a comment |Â
add a comment |Â
up vote
2
down vote
Ruby, 89 82 bytes
Creates the cross-product of the input list against itself before calculating the Hamming distance of each pair, using a duplication method similar to Chas Brown's answer. Ruby can't zip strings together or add booleans without additional overhead, though, so it becomes necessary to iterate through the pair of strings manually instead.
-7 bytes from GB.
->aa.product(a).maps,t.max
Try it online!
1
82 bytes
â G B
Aug 14 at 11:27
add a comment |Â
up vote
2
down vote
Ruby, 89 82 bytes
Creates the cross-product of the input list against itself before calculating the Hamming distance of each pair, using a duplication method similar to Chas Brown's answer. Ruby can't zip strings together or add booleans without additional overhead, though, so it becomes necessary to iterate through the pair of strings manually instead.
-7 bytes from GB.
->aa.product(a).maps,t.max
Try it online!
1
82 bytes
â G B
Aug 14 at 11:27
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Ruby, 89 82 bytes
Creates the cross-product of the input list against itself before calculating the Hamming distance of each pair, using a duplication method similar to Chas Brown's answer. Ruby can't zip strings together or add booleans without additional overhead, though, so it becomes necessary to iterate through the pair of strings manually instead.
-7 bytes from GB.
->aa.product(a).maps,t.max
Try it online!
Ruby, 89 82 bytes
Creates the cross-product of the input list against itself before calculating the Hamming distance of each pair, using a duplication method similar to Chas Brown's answer. Ruby can't zip strings together or add booleans without additional overhead, though, so it becomes necessary to iterate through the pair of strings manually instead.
-7 bytes from GB.
->aa.product(a).maps,t.max
Try it online!
edited Aug 14 at 22:59
answered Aug 14 at 3:31
Value Ink
7,827630
7,827630
1
82 bytes
â G B
Aug 14 at 11:27
add a comment |Â
1
82 bytes
â G B
Aug 14 at 11:27
1
1
82 bytes
â G B
Aug 14 at 11:27
82 bytes
â G B
Aug 14 at 11:27
add a comment |Â
up vote
2
down vote
Java 10, 748 740 667 666 616 bytes
This has to be the most dense and unreadable, yet the longest golf I ever came up with.
Call method h(String)
with an explicit array (no var args): eg,
h(new String "a", "b", "c");
returns 1
.
char e(boolean w,char c)96<c&c<123)?c^32:c);String p(String s,int l)var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;int d(String s,String t)int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;int h(String s,String t)int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);int h(Strings)int l=s.length,i=0,j;intn=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();
You can Try It Online!
Ungolfed and commented:
// Encode the character (swap case)
char e(boolean w, char c) 96 < c & c < 123) ? c ^ 32 : c);
// Pad the string to desired length
String p(String s, int l)
var p = "";
int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
var w = 1 < 0;
for (; i < n; ++i, w = !w)
for (char c : s.toCharArray())
p += e(w, c);
for (; j < r;)
p += e(w, s.charAt(j++));
return p;
// Calculate the actual hamming distance between two same-length strings
int d(String s, String t)
int l = s.length(), n = 0, i = 0;
for (; i < l;)
if (s.charAt(i) != t.charAt(i++))
++n;
return n;
// Pad the strings as needed and return their hamming distance
int h(String s, String t)
int l = s.length(), m = t.length();
return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
// Dispatch the strings and gather their hamming distances, return the max
int h(String s)
int l = s.length, i = 0, j;
int n = new int[l * l];
for (; i < l; ++i)
for (j = i; ++j < l;)
n[i * l + j] = h(s[i], s[j]);
return java.util.Arrays.stream(n).max().getAsInt();
I know a better solution can be achieved, especially for the string pairing part.
EDIT: shave off 8 bytes by changing the size of the int array in hammingDistance()
to the square of the numbe of strings given. It also fixes an ArrayIndexOutOfBounds
thrown in one of the test cases.
EDIT 2: Saved 33 bytes thanks to Kevin Cruijssen's comments: class declaration removed, names shortened to 1 char, operators changed, etc.
EDIT 3: Save 1 byte and reach Satan-approved score by changing method with var-arg to array.
EDIT 4: Save another 50 bytes thanks to Kevin Cruijssen, again: update Java version from 8 to 10 to use var
keyword, removed StringBuilder
instance, etc.
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead ofhammingDistance
used
or some other unused variable. Most of your&&
can be&
and||
can be|
.c^' '
can bec^32
.boolean w = false;
can beboolean w=0>1;
.i=0
in the loop initialization can be removed and change the,i,j
to,i=0,j
.++j
can be removed and++
can be added to the.charAt(j++)
..toString()
can be+""
.for(j=i+1;j<l;++j)
can befor(j=0;++j<l;)
. Etc. etc.
â Kevin Cruijssen
Aug 14 at 13:38
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
Upvoted for the Satan-approved score. xD Some more small things:StringBuilder
can beStringBuffer
(if you switch to Java 10 it could bevar b=new StringBuffer(l);
. Theboolean
andchar
can then also bevar
. If you don't have Java 10 locally, it is available on TIO). In addition,for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can befor(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove theStringBuffer
completely and just useString
and+=
instead ofappend
.
â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
add a comment |Â
up vote
2
down vote
Java 10, 748 740 667 666 616 bytes
This has to be the most dense and unreadable, yet the longest golf I ever came up with.
Call method h(String)
with an explicit array (no var args): eg,
h(new String "a", "b", "c");
returns 1
.
char e(boolean w,char c)96<c&c<123)?c^32:c);String p(String s,int l)var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;int d(String s,String t)int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;int h(String s,String t)int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);int h(Strings)int l=s.length,i=0,j;intn=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();
You can Try It Online!
Ungolfed and commented:
// Encode the character (swap case)
char e(boolean w, char c) 96 < c & c < 123) ? c ^ 32 : c);
// Pad the string to desired length
String p(String s, int l)
var p = "";
int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
var w = 1 < 0;
for (; i < n; ++i, w = !w)
for (char c : s.toCharArray())
p += e(w, c);
for (; j < r;)
p += e(w, s.charAt(j++));
return p;
// Calculate the actual hamming distance between two same-length strings
int d(String s, String t)
int l = s.length(), n = 0, i = 0;
for (; i < l;)
if (s.charAt(i) != t.charAt(i++))
++n;
return n;
// Pad the strings as needed and return their hamming distance
int h(String s, String t)
int l = s.length(), m = t.length();
return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
// Dispatch the strings and gather their hamming distances, return the max
int h(String s)
int l = s.length, i = 0, j;
int n = new int[l * l];
for (; i < l; ++i)
for (j = i; ++j < l;)
n[i * l + j] = h(s[i], s[j]);
return java.util.Arrays.stream(n).max().getAsInt();
I know a better solution can be achieved, especially for the string pairing part.
EDIT: shave off 8 bytes by changing the size of the int array in hammingDistance()
to the square of the numbe of strings given. It also fixes an ArrayIndexOutOfBounds
thrown in one of the test cases.
EDIT 2: Saved 33 bytes thanks to Kevin Cruijssen's comments: class declaration removed, names shortened to 1 char, operators changed, etc.
EDIT 3: Save 1 byte and reach Satan-approved score by changing method with var-arg to array.
EDIT 4: Save another 50 bytes thanks to Kevin Cruijssen, again: update Java version from 8 to 10 to use var
keyword, removed StringBuilder
instance, etc.
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead ofhammingDistance
used
or some other unused variable. Most of your&&
can be&
and||
can be|
.c^' '
can bec^32
.boolean w = false;
can beboolean w=0>1;
.i=0
in the loop initialization can be removed and change the,i,j
to,i=0,j
.++j
can be removed and++
can be added to the.charAt(j++)
..toString()
can be+""
.for(j=i+1;j<l;++j)
can befor(j=0;++j<l;)
. Etc. etc.
â Kevin Cruijssen
Aug 14 at 13:38
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
Upvoted for the Satan-approved score. xD Some more small things:StringBuilder
can beStringBuffer
(if you switch to Java 10 it could bevar b=new StringBuffer(l);
. Theboolean
andchar
can then also bevar
. If you don't have Java 10 locally, it is available on TIO). In addition,for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can befor(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove theStringBuffer
completely and just useString
and+=
instead ofappend
.
â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Java 10, 748 740 667 666 616 bytes
This has to be the most dense and unreadable, yet the longest golf I ever came up with.
Call method h(String)
with an explicit array (no var args): eg,
h(new String "a", "b", "c");
returns 1
.
char e(boolean w,char c)96<c&c<123)?c^32:c);String p(String s,int l)var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;int d(String s,String t)int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;int h(String s,String t)int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);int h(Strings)int l=s.length,i=0,j;intn=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();
You can Try It Online!
Ungolfed and commented:
// Encode the character (swap case)
char e(boolean w, char c) 96 < c & c < 123) ? c ^ 32 : c);
// Pad the string to desired length
String p(String s, int l)
var p = "";
int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
var w = 1 < 0;
for (; i < n; ++i, w = !w)
for (char c : s.toCharArray())
p += e(w, c);
for (; j < r;)
p += e(w, s.charAt(j++));
return p;
// Calculate the actual hamming distance between two same-length strings
int d(String s, String t)
int l = s.length(), n = 0, i = 0;
for (; i < l;)
if (s.charAt(i) != t.charAt(i++))
++n;
return n;
// Pad the strings as needed and return their hamming distance
int h(String s, String t)
int l = s.length(), m = t.length();
return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
// Dispatch the strings and gather their hamming distances, return the max
int h(String s)
int l = s.length, i = 0, j;
int n = new int[l * l];
for (; i < l; ++i)
for (j = i; ++j < l;)
n[i * l + j] = h(s[i], s[j]);
return java.util.Arrays.stream(n).max().getAsInt();
I know a better solution can be achieved, especially for the string pairing part.
EDIT: shave off 8 bytes by changing the size of the int array in hammingDistance()
to the square of the numbe of strings given. It also fixes an ArrayIndexOutOfBounds
thrown in one of the test cases.
EDIT 2: Saved 33 bytes thanks to Kevin Cruijssen's comments: class declaration removed, names shortened to 1 char, operators changed, etc.
EDIT 3: Save 1 byte and reach Satan-approved score by changing method with var-arg to array.
EDIT 4: Save another 50 bytes thanks to Kevin Cruijssen, again: update Java version from 8 to 10 to use var
keyword, removed StringBuilder
instance, etc.
Java 10, 748 740 667 666 616 bytes
This has to be the most dense and unreadable, yet the longest golf I ever came up with.
Call method h(String)
with an explicit array (no var args): eg,
h(new String "a", "b", "c");
returns 1
.
char e(boolean w,char c)96<c&c<123)?c^32:c);String p(String s,int l)var p="";int m=s.length(),n=l/m,r=l%m,i=0,j=0;var w=1<0;for(;i<n;++i,w=!w)for(char c:s.toCharArray())p+=e(w,c);for(;j<r;)p+=e(w,s.charAt(j++));return p;int d(String s,String t)int l=s.length(),n=0,i=0;for(;i<l;)if(s.charAt(i)!=t.charAt(i++))++n;return n;int h(String s,String t)int l=s.length(),m=t.length();return l>m?d(s,p(t,l)):l<m?d(p(s,m),t):d(s,t);int h(Strings)int l=s.length,i=0,j;intn=new int[l*l];for(;i<l;++i)for(j=i;++j<l;)n[i*l+j]=h(s[i],s[j]);return java.util.Arrays.stream(n).max().getAsInt();
You can Try It Online!
Ungolfed and commented:
// Encode the character (swap case)
char e(boolean w, char c) 96 < c & c < 123) ? c ^ 32 : c);
// Pad the string to desired length
String p(String s, int l)
var p = "";
int m = s.length(), n = l / m, r = l % m, i = 0, j = 0;
var w = 1 < 0;
for (; i < n; ++i, w = !w)
for (char c : s.toCharArray())
p += e(w, c);
for (; j < r;)
p += e(w, s.charAt(j++));
return p;
// Calculate the actual hamming distance between two same-length strings
int d(String s, String t)
int l = s.length(), n = 0, i = 0;
for (; i < l;)
if (s.charAt(i) != t.charAt(i++))
++n;
return n;
// Pad the strings as needed and return their hamming distance
int h(String s, String t)
int l = s.length(), m = t.length();
return l > m ? d(s, p(t, l)) : l < m ? d(p(s, m), t) : d(s, t);
// Dispatch the strings and gather their hamming distances, return the max
int h(String s)
int l = s.length, i = 0, j;
int n = new int[l * l];
for (; i < l; ++i)
for (j = i; ++j < l;)
n[i * l + j] = h(s[i], s[j]);
return java.util.Arrays.stream(n).max().getAsInt();
I know a better solution can be achieved, especially for the string pairing part.
EDIT: shave off 8 bytes by changing the size of the int array in hammingDistance()
to the square of the numbe of strings given. It also fixes an ArrayIndexOutOfBounds
thrown in one of the test cases.
EDIT 2: Saved 33 bytes thanks to Kevin Cruijssen's comments: class declaration removed, names shortened to 1 char, operators changed, etc.
EDIT 3: Save 1 byte and reach Satan-approved score by changing method with var-arg to array.
EDIT 4: Save another 50 bytes thanks to Kevin Cruijssen, again: update Java version from 8 to 10 to use var
keyword, removed StringBuilder
instance, etc.
edited Aug 16 at 8:35
answered Aug 14 at 9:39
joH1
286210
286210
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead ofhammingDistance
used
or some other unused variable. Most of your&&
can be&
and||
can be|
.c^' '
can bec^32
.boolean w = false;
can beboolean w=0>1;
.i=0
in the loop initialization can be removed and change the,i,j
to,i=0,j
.++j
can be removed and++
can be added to the.charAt(j++)
..toString()
can be+""
.for(j=i+1;j<l;++j)
can befor(j=0;++j<l;)
. Etc. etc.
â Kevin Cruijssen
Aug 14 at 13:38
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
Upvoted for the Satan-approved score. xD Some more small things:StringBuilder
can beStringBuffer
(if you switch to Java 10 it could bevar b=new StringBuffer(l);
. Theboolean
andchar
can then also bevar
. If you don't have Java 10 locally, it is available on TIO). In addition,for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can befor(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove theStringBuffer
completely and just useString
and+=
instead ofappend
.
â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
add a comment |Â
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead ofhammingDistance
used
or some other unused variable. Most of your&&
can be&
and||
can be|
.c^' '
can bec^32
.boolean w = false;
can beboolean w=0>1;
.i=0
in the loop initialization can be removed and change the,i,j
to,i=0,j
.++j
can be removed and++
can be added to the.charAt(j++)
..toString()
can be+""
.for(j=i+1;j<l;++j)
can befor(j=0;++j<l;)
. Etc. etc.
â Kevin Cruijssen
Aug 14 at 13:38
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
Upvoted for the Satan-approved score. xD Some more small things:StringBuilder
can beStringBuffer
(if you switch to Java 10 it could bevar b=new StringBuffer(l);
. Theboolean
andchar
can then also bevar
. If you don't have Java 10 locally, it is available on TIO). In addition,for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can befor(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove theStringBuffer
completely and just useString
and+=
instead ofappend
.
â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
1
1
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead of
hammingDistance
use d
or some other unused variable. Most of your &&
can be &
and ||
can be |
. c^' '
can be c^32
. boolean w = false;
can be boolean w=0>1;
. i=0
in the loop initialization can be removed and change the ,i,j
to ,i=0,j
. ++j
can be removed and ++
can be added to the .charAt(j++)
. .toString()
can be +""
. for(j=i+1;j<l;++j)
can be for(j=0;++j<l;)
. Etc. etc.â Kevin Cruijssen
Aug 14 at 13:38
I don't have a lot of time, but some basic things to golf: Drop the class, only the methods is enough. Change all method and variable names to single bytes.. So instead of
hammingDistance
use d
or some other unused variable. Most of your &&
can be &
and ||
can be |
. c^' '
can be c^32
. boolean w = false;
can be boolean w=0>1;
. i=0
in the loop initialization can be removed and change the ,i,j
to ,i=0,j
. ++j
can be removed and ++
can be added to the .charAt(j++)
. .toString()
can be +""
. for(j=i+1;j<l;++j)
can be for(j=0;++j<l;)
. Etc. etc.â Kevin Cruijssen
Aug 14 at 13:38
1
1
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Tips for golfing in Java and Tips for golfing in <all languages> might be interesting to read through as well. :)
â Kevin Cruijssen
Aug 14 at 13:41
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
Thanks! That's some nice byte-lifting. Thanks for the links too, I'm taking a look at it and will edit asap!
â joH1
Aug 14 at 13:47
1
1
Upvoted for the Satan-approved score. xD Some more small things:
StringBuilder
can be StringBuffer
(if you switch to Java 10 it could be var b=new StringBuffer(l);
. The boolean
and char
can then also be var
. If you don't have Java 10 locally, it is available on TIO). In addition, for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can be for(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove the StringBuffer
completely and just use String
and +=
instead of append
.â Kevin Cruijssen
Aug 14 at 15:13
Upvoted for the Satan-approved score. xD Some more small things:
StringBuilder
can be StringBuffer
(if you switch to Java 10 it could be var b=new StringBuffer(l);
. The boolean
and char
can then also be var
. If you don't have Java 10 locally, it is available on TIO). In addition, for(;i<n;++i)for(char c:s.toCharArray())b.append(e(w,c));w=!w;
can be for(;i++<n;w=!w)for(char c:s.toCharArray())b.append(e(w,c));
. And I'm pretty sure you can remove the StringBuffer
completely and just use String
and +=
instead of append
.â Kevin Cruijssen
Aug 14 at 15:13
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
Man, some many months of clean code and good coding practices made me forget how to even golf! I'll update my answer and include TIO.
â joH1
Aug 16 at 7:15
add a comment |Â
up vote
1
down vote
05AB1E, 33 29 bytes
ÃÂü)â¬ééõÃÂDÃ
¡Ã«s`gâÂÂ}îâŠâÂÂøâ¬ö≬ÃÂ_OÃÂ
Try it online or verify all test cases.
Can most likely be halved in byte-count, but it works..
Explanation:
ÃÂ # Enclose the input-list (adding the first item to the end of the list)
# i.e. ['ABC1','abcD','abCd32e'] â ['ABC1','abcD','abCd32e','ABC1']
ü) # Pair-vectorize each of them
# i.e. ['ABC1','abcD','abCd32e','ABC1']
# â [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
⎠# Sort each pair by length
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
é # Store this list in the register to re-use later on
õ } # Map each inner list in this list to:
ÃÂ # Head extracted
# i.e. ['abcD','abCd32e'] â 'abcD' and ['abCd32e']
DÃ
¡ # Duplicate it, and swap the capitalization of the copy
# i.e. 'abcD' â 'ABCd'
ë # Then merge it together
# i.e. 'abcD' and 'ABCd' â 'abcDABCd'
s` # Swap so the tail-list is at the top of the stack, and get it's single item
# i.e. ['abCd32e'] â 'abCd32e'
g # Get the length of that
# i.e. 'abCd32e' â 7
â # Extend/shorten the string to that length
# i.e. 'abcDABCd' and 7 â 'abcDABC'
î # Get the saved list from the register again
⊠# Get the tail from each
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â ['abcD','abCd32e','abCd32e']
â # Pair it with the other list
# i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
# â [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
ø # Zip it, swapping rows / columns
# i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
# â [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
â¬ö # And then zip each pair again
# i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
# â [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
⬠# Then for each inner list
⬠# And for each inner string
ÃÂ # Check if all characters are the same
# i.e. 'aa' â 1
# i.e. 'cC' â 0
_ # And inverse the booleans
# i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
# â [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O # Then sum each inner list
# i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] â [4,5,6]
ÃÂ # And take the max as result
# i.e. [4,5,6] â 6
add a comment |Â
up vote
1
down vote
05AB1E, 33 29 bytes
ÃÂü)â¬ééõÃÂDÃ
¡Ã«s`gâÂÂ}îâŠâÂÂøâ¬ö≬ÃÂ_OÃÂ
Try it online or verify all test cases.
Can most likely be halved in byte-count, but it works..
Explanation:
ÃÂ # Enclose the input-list (adding the first item to the end of the list)
# i.e. ['ABC1','abcD','abCd32e'] â ['ABC1','abcD','abCd32e','ABC1']
ü) # Pair-vectorize each of them
# i.e. ['ABC1','abcD','abCd32e','ABC1']
# â [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
⎠# Sort each pair by length
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
é # Store this list in the register to re-use later on
õ } # Map each inner list in this list to:
ÃÂ # Head extracted
# i.e. ['abcD','abCd32e'] â 'abcD' and ['abCd32e']
DÃ
¡ # Duplicate it, and swap the capitalization of the copy
# i.e. 'abcD' â 'ABCd'
ë # Then merge it together
# i.e. 'abcD' and 'ABCd' â 'abcDABCd'
s` # Swap so the tail-list is at the top of the stack, and get it's single item
# i.e. ['abCd32e'] â 'abCd32e'
g # Get the length of that
# i.e. 'abCd32e' â 7
â # Extend/shorten the string to that length
# i.e. 'abcDABCd' and 7 â 'abcDABC'
î # Get the saved list from the register again
⊠# Get the tail from each
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â ['abcD','abCd32e','abCd32e']
â # Pair it with the other list
# i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
# â [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
ø # Zip it, swapping rows / columns
# i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
# â [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
â¬ö # And then zip each pair again
# i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
# â [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
⬠# Then for each inner list
⬠# And for each inner string
ÃÂ # Check if all characters are the same
# i.e. 'aa' â 1
# i.e. 'cC' â 0
_ # And inverse the booleans
# i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
# â [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O # Then sum each inner list
# i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] â [4,5,6]
ÃÂ # And take the max as result
# i.e. [4,5,6] â 6
add a comment |Â
up vote
1
down vote
up vote
1
down vote
05AB1E, 33 29 bytes
ÃÂü)â¬ééõÃÂDÃ
¡Ã«s`gâÂÂ}îâŠâÂÂøâ¬ö≬ÃÂ_OÃÂ
Try it online or verify all test cases.
Can most likely be halved in byte-count, but it works..
Explanation:
ÃÂ # Enclose the input-list (adding the first item to the end of the list)
# i.e. ['ABC1','abcD','abCd32e'] â ['ABC1','abcD','abCd32e','ABC1']
ü) # Pair-vectorize each of them
# i.e. ['ABC1','abcD','abCd32e','ABC1']
# â [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
⎠# Sort each pair by length
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
é # Store this list in the register to re-use later on
õ } # Map each inner list in this list to:
ÃÂ # Head extracted
# i.e. ['abcD','abCd32e'] â 'abcD' and ['abCd32e']
DÃ
¡ # Duplicate it, and swap the capitalization of the copy
# i.e. 'abcD' â 'ABCd'
ë # Then merge it together
# i.e. 'abcD' and 'ABCd' â 'abcDABCd'
s` # Swap so the tail-list is at the top of the stack, and get it's single item
# i.e. ['abCd32e'] â 'abCd32e'
g # Get the length of that
# i.e. 'abCd32e' â 7
â # Extend/shorten the string to that length
# i.e. 'abcDABCd' and 7 â 'abcDABC'
î # Get the saved list from the register again
⊠# Get the tail from each
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â ['abcD','abCd32e','abCd32e']
â # Pair it with the other list
# i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
# â [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
ø # Zip it, swapping rows / columns
# i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
# â [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
â¬ö # And then zip each pair again
# i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
# â [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
⬠# Then for each inner list
⬠# And for each inner string
ÃÂ # Check if all characters are the same
# i.e. 'aa' â 1
# i.e. 'cC' â 0
_ # And inverse the booleans
# i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
# â [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O # Then sum each inner list
# i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] â [4,5,6]
ÃÂ # And take the max as result
# i.e. [4,5,6] â 6
05AB1E, 33 29 bytes
ÃÂü)â¬ééõÃÂDÃ
¡Ã«s`gâÂÂ}îâŠâÂÂøâ¬ö≬ÃÂ_OÃÂ
Try it online or verify all test cases.
Can most likely be halved in byte-count, but it works..
Explanation:
ÃÂ # Enclose the input-list (adding the first item to the end of the list)
# i.e. ['ABC1','abcD','abCd32e'] â ['ABC1','abcD','abCd32e','ABC1']
ü) # Pair-vectorize each of them
# i.e. ['ABC1','abcD','abCd32e','ABC1']
# â [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
⎠# Sort each pair by length
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â [['ABC1','abcD'],['abcD','abCd32e'],['ABC1','abCd32e']]
é # Store this list in the register to re-use later on
õ } # Map each inner list in this list to:
ÃÂ # Head extracted
# i.e. ['abcD','abCd32e'] â 'abcD' and ['abCd32e']
DÃ
¡ # Duplicate it, and swap the capitalization of the copy
# i.e. 'abcD' â 'ABCd'
ë # Then merge it together
# i.e. 'abcD' and 'ABCd' â 'abcDABCd'
s` # Swap so the tail-list is at the top of the stack, and get it's single item
# i.e. ['abCd32e'] â 'abCd32e'
g # Get the length of that
# i.e. 'abCd32e' â 7
â # Extend/shorten the string to that length
# i.e. 'abcDABCd' and 7 â 'abcDABC'
î # Get the saved list from the register again
⊠# Get the tail from each
# i.e. [['ABC1','abcD'],['abcD','abCd32e'],['abCd32e','ABC1']]
# â ['abcD','abCd32e','abCd32e']
â # Pair it with the other list
# i.e. ['ABC1','abcDABC','ABC1abc'] and ['abcD','abCd32e','abCd32e']
# â [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
ø # Zip it, swapping rows / columns
# i.e. [['ABC1','abcDABC','ABC1abc'],['abcD','abCd32e','abCd32e']]
# â [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
â¬ö # And then zip each pair again
# i.e. [['ABC1','abcD'],['abcDABC','abCd32e'],['ABC1abc','abCd32e']]
# â [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
⬠# Then for each inner list
⬠# And for each inner string
ÃÂ # Check if all characters are the same
# i.e. 'aa' â 1
# i.e. 'cC' â 0
_ # And inverse the booleans
# i.e. [['Aa','Bb','Cc','1D'],['aa','bb','cC','Dd','A3','B2','Ce'],['Aa','Bb','CC','1d','a3','b2','ce']]
# â [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]]
O # Then sum each inner list
# i.e. [[1,1,1,1],[0,0,1,1,1,1,1],[1,1,0,1,1,1,1]] â [4,5,6]
ÃÂ # And take the max as result
# i.e. [4,5,6] â 6
answered Aug 14 at 9:30
Kevin Cruijssen
29.4k548162
29.4k548162
add a comment |Â
add a comment |Â
up vote
1
down vote
Java 11, 387 bytes
a->int l=a.length,i=l,t,j=0,C=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p)var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91for(int c:C)j=c>j?c:j;return j;
Try it online. (NOTE: Since Java 11 isn't on TIO yet, String.repeat(int)
has been emulated as repeat(String,int)
for the same byte-count.)
Explanation:
a-> // Method with String-array parameter and integer return-type
int l=a.length, // Length of the input-array
i=l, // Index-integer, starting at the length
t,j=0, // Temp-integers
C=new int[l]; // Count-array the same size as the input
var p=new String[l][2]; // String-pairs array the same size as the input
for(;i-->0 // Loop `i` in the range [`l`, 0)
; // After every iteration:
p[i][0]= // Set the first String of the pair at index `i` to:
a[t>0?i:j],// The smallest of the `i`'th or `j`'th Strings of the input-array
p[i][1]= // And set the second String of the pair at index `i` to:
a[t>0?j:i])// The largest of the `i`'th or `j`'th Strings of the input-array
t=a[i].length()< // If the length of the `i`'th item is smaller than
a[j=-~i%l].length()?// the length of the `i+1`'th item
// (and set `j` to this `i+1` with wrap-around to 0 for the last item
1 // Set `t` to 1 as flag
: // Else:
0; // Set `t` to 0 as flag
// We've now created the String pairs, where each pair is sorted by length
i=0; // Reset `i` to 0
for(var P:p) // Loop over the pairs
var s=""; // Temp-String starting empty
for(var x:P[0].getBytes())
// Loop over the characters of the first String of the pair
s+= // Append the temp-String with:
(char)(x>64&x<91 // After every iteration of the outer loop,
// increase `i` by 1 for the next iteration
for(int c:C) // Now loop over the calculated counts
j=c>j?c:j; // And set `j` to the maximum
return j; // And finally return this maximum `j` as result
add a comment |Â
up vote
1
down vote
Java 11, 387 bytes
a->int l=a.length,i=l,t,j=0,C=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p)var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91for(int c:C)j=c>j?c:j;return j;
Try it online. (NOTE: Since Java 11 isn't on TIO yet, String.repeat(int)
has been emulated as repeat(String,int)
for the same byte-count.)
Explanation:
a-> // Method with String-array parameter and integer return-type
int l=a.length, // Length of the input-array
i=l, // Index-integer, starting at the length
t,j=0, // Temp-integers
C=new int[l]; // Count-array the same size as the input
var p=new String[l][2]; // String-pairs array the same size as the input
for(;i-->0 // Loop `i` in the range [`l`, 0)
; // After every iteration:
p[i][0]= // Set the first String of the pair at index `i` to:
a[t>0?i:j],// The smallest of the `i`'th or `j`'th Strings of the input-array
p[i][1]= // And set the second String of the pair at index `i` to:
a[t>0?j:i])// The largest of the `i`'th or `j`'th Strings of the input-array
t=a[i].length()< // If the length of the `i`'th item is smaller than
a[j=-~i%l].length()?// the length of the `i+1`'th item
// (and set `j` to this `i+1` with wrap-around to 0 for the last item
1 // Set `t` to 1 as flag
: // Else:
0; // Set `t` to 0 as flag
// We've now created the String pairs, where each pair is sorted by length
i=0; // Reset `i` to 0
for(var P:p) // Loop over the pairs
var s=""; // Temp-String starting empty
for(var x:P[0].getBytes())
// Loop over the characters of the first String of the pair
s+= // Append the temp-String with:
(char)(x>64&x<91 // After every iteration of the outer loop,
// increase `i` by 1 for the next iteration
for(int c:C) // Now loop over the calculated counts
j=c>j?c:j; // And set `j` to the maximum
return j; // And finally return this maximum `j` as result
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Java 11, 387 bytes
a->int l=a.length,i=l,t,j=0,C=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p)var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91for(int c:C)j=c>j?c:j;return j;
Try it online. (NOTE: Since Java 11 isn't on TIO yet, String.repeat(int)
has been emulated as repeat(String,int)
for the same byte-count.)
Explanation:
a-> // Method with String-array parameter and integer return-type
int l=a.length, // Length of the input-array
i=l, // Index-integer, starting at the length
t,j=0, // Temp-integers
C=new int[l]; // Count-array the same size as the input
var p=new String[l][2]; // String-pairs array the same size as the input
for(;i-->0 // Loop `i` in the range [`l`, 0)
; // After every iteration:
p[i][0]= // Set the first String of the pair at index `i` to:
a[t>0?i:j],// The smallest of the `i`'th or `j`'th Strings of the input-array
p[i][1]= // And set the second String of the pair at index `i` to:
a[t>0?j:i])// The largest of the `i`'th or `j`'th Strings of the input-array
t=a[i].length()< // If the length of the `i`'th item is smaller than
a[j=-~i%l].length()?// the length of the `i+1`'th item
// (and set `j` to this `i+1` with wrap-around to 0 for the last item
1 // Set `t` to 1 as flag
: // Else:
0; // Set `t` to 0 as flag
// We've now created the String pairs, where each pair is sorted by length
i=0; // Reset `i` to 0
for(var P:p) // Loop over the pairs
var s=""; // Temp-String starting empty
for(var x:P[0].getBytes())
// Loop over the characters of the first String of the pair
s+= // Append the temp-String with:
(char)(x>64&x<91 // After every iteration of the outer loop,
// increase `i` by 1 for the next iteration
for(int c:C) // Now loop over the calculated counts
j=c>j?c:j; // And set `j` to the maximum
return j; // And finally return this maximum `j` as result
Java 11, 387 bytes
a->int l=a.length,i=l,t,j=0,C=new int[l];var p=new String[l][2];for(;i-->0;p[i][0]=a[t>0?i:j],p[i][1]=a[t>0?j:i])t=a[i].length()<a[j=-~i%l].length()?1:0;i=0;for(var P:p)var s="";for(var x:P[0].getBytes())s+=(char)(x>64&x<91for(int c:C)j=c>j?c:j;return j;
Try it online. (NOTE: Since Java 11 isn't on TIO yet, String.repeat(int)
has been emulated as repeat(String,int)
for the same byte-count.)
Explanation:
a-> // Method with String-array parameter and integer return-type
int l=a.length, // Length of the input-array
i=l, // Index-integer, starting at the length
t,j=0, // Temp-integers
C=new int[l]; // Count-array the same size as the input
var p=new String[l][2]; // String-pairs array the same size as the input
for(;i-->0 // Loop `i` in the range [`l`, 0)
; // After every iteration:
p[i][0]= // Set the first String of the pair at index `i` to:
a[t>0?i:j],// The smallest of the `i`'th or `j`'th Strings of the input-array
p[i][1]= // And set the second String of the pair at index `i` to:
a[t>0?j:i])// The largest of the `i`'th or `j`'th Strings of the input-array
t=a[i].length()< // If the length of the `i`'th item is smaller than
a[j=-~i%l].length()?// the length of the `i+1`'th item
// (and set `j` to this `i+1` with wrap-around to 0 for the last item
1 // Set `t` to 1 as flag
: // Else:
0; // Set `t` to 0 as flag
// We've now created the String pairs, where each pair is sorted by length
i=0; // Reset `i` to 0
for(var P:p) // Loop over the pairs
var s=""; // Temp-String starting empty
for(var x:P[0].getBytes())
// Loop over the characters of the first String of the pair
s+= // Append the temp-String with:
(char)(x>64&x<91 // After every iteration of the outer loop,
// increase `i` by 1 for the next iteration
for(int c:C) // Now loop over the calculated counts
j=c>j?c:j; // And set `j` to the maximum
return j; // And finally return this maximum `j` as result
answered Aug 14 at 14:40
Kevin Cruijssen
29.4k548162
29.4k548162
add a comment |Â
add a comment |Â
up vote
1
down vote
R, 173 bytes
function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))
Try it online!
@ngm : I tried my best to golf your code (with my heavy customizations of course) but, as you well know, R is not very golfy in manipulating strings :P
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might includeouter
to get all the combinations, and doing modular arithmetic on the code points in lieu ofchartr
.
â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
add a comment |Â
up vote
1
down vote
R, 173 bytes
function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))
Try it online!
@ngm : I tried my best to golf your code (with my heavy customizations of course) but, as you well know, R is not very golfy in manipulating strings :P
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might includeouter
to get all the combinations, and doing modular arithmetic on the code points in lieu ofchartr
.
â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
add a comment |Â
up vote
1
down vote
up vote
1
down vote
R, 173 bytes
function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))
Try it online!
@ngm : I tried my best to golf your code (with my heavy customizations of course) but, as you well know, R is not very golfy in manipulating strings :P
R, 173 bytes
function(x,U=utf8ToInt,N=nchar)max(combn(x,2,function(z,v=z[order(N(z))])sum(U(substr(Reduce(paste0,rep(c(v[1],chartr('A-Za-z','a-zA-Z',v[1])),n<-N(v[2]))),1,n))!=U(v[2]))))
Try it online!
@ngm : I tried my best to golf your code (with my heavy customizations of course) but, as you well know, R is not very golfy in manipulating strings :P
edited Aug 15 at 17:06
answered Aug 15 at 7:41
digEmAll
1,71147
1,71147
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might includeouter
to get all the combinations, and doing modular arithmetic on the code points in lieu ofchartr
.
â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
add a comment |Â
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might includeouter
to get all the combinations, and doing modular arithmetic on the code points in lieu ofchartr
.
â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
I bet this can be sub 150 bytes, but I'm not sure how just yet.
â Giuseppe
Aug 15 at 17:50
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@Giuseppe: I suspect that too... but I'm not really good in writing short strings manipulation codes and R doesn't help me very much either :D
â digEmAll
Aug 15 at 18:56
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might include
outer
to get all the combinations, and doing modular arithmetic on the code points in lieu of chartr
.â ngm
Aug 16 at 0:33
@digEmAll I'm not going to try to solve my own challenge, but few possibilities might include
outer
to get all the combinations, and doing modular arithmetic on the code points in lieu of chartr
.â ngm
Aug 16 at 0:33
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
@ngm: possible...I discarded the arithmetic approach because I couldn't find a short solution/formula to change the case for letters without touching the numbers...
â digEmAll
Aug 16 at 6:18
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170581%2fmaximum-hamming-distance-among-a-list-of-padded-strings%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
change the cases of the letters each odd time wrapping â Oh boy, this requirement is going to be a pain for my solution... I like the challenge, though, so +1
â Mr. Xcoder
Aug 13 at 19:42
Suggested test case:
["AacaAc", "Aab"] => 2
. A purposed golf to my Jelly answer would have failed that case, but would have passes all the other ones.â Mr. Xcoder
Aug 13 at 21:57
@ngm Excellent challenge! +1
â Rushabh Mehta
Aug 14 at 15:46