Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Multiply two strings

+3
−0

Given two strings, I define their product as follows:

  • If any of the two strings is empty, the product is the empty string.

  • If the second string consists of a single character, the result is obtained by replacing every character of the first string that compares larger than that character with that character. For example, "1234567" * "5" = "1234555"

  • If the second string consists of more than one character, the result is obtained by multiplying the first string with each one-letter substring and concatenating the results. For example, s * "12" = s * "1" + s * "2" where + denotes string concatenation.

The task is to write a program or function that takes two strings and returns or prints their product. It may be assumed that the input strings consist entirely of printable ASCII characters.

You are allowed to take the second string as first argument and the first string as second argument as long as you explicitly state it in your answer (and are consistent about it).

This is code golf, the shortest code wins.

Test cases:

"" * "" = ""
"" * "123" = ""
"12" * "" = ""
"2" * "3" = "2"
"5" * "4" = "4"
"//" * "///" = "//////"
"xxxx" * "xx" = "xxxxxxxx"
"12" * "123" = "111212"
"123" * "abc" = "123123123"
"abc" * "123" = "111222333"
"abc" * "abc" = "aaaabbabc"

Note: While this task is inspired by a string multiplication question I encountered Somewhere Else a while ago, the algorithm used here is not the same as the algorithm in that other question.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

6 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

Japt -m, 5 bytes

Takes input in reverse order. Sadly, Japt has no minimum built-in for strings.

VcmUc

Try it

VcmUc     :Implicit map of each U in the first input string
V         :Second input
 c        :Map charcodes
  m       :  Minimum with
   Uc     :  Charcode of U
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

BQN, 3 bytes

Anonymous tacit infix function, taking the second string as left argument and the first string as right argument.

⥊⌊⌜

⌊⌜ minimum table

 deshape (flatten)

BQN treats characters as an affine space which is basically a fancy name for each character being an integer (indicating the code point) and a Boolean (indicating the "characterness": 1 for character, 0 for non-character). Arithmetic on characters applies to both the code point and the characterness. Here, we take the minimum which gives the desired code point, and since both arguments are characters, the characterness stays at 1.

Try it!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Jelly, 2 bytes

«þ

Try it online!

Technically speaking, this needs to be a full program, due to how Jelly's smash printing works, but it can get away with being run as a function over test cases.

Essentially, this generates a matrix $M$ where $M_{ij}$ is given by $\min(a_i, b_j)$. As this is a matrix of characters, Jelly automatically flattens and concatenates the string on output

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

JavaScript, 37 bytes

I/O as character arrays.

a=>b=>b.flatMap(x=>a.map(y=>x<y?x:y))

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Haskell, 25 bytes

f a=(>>= \c->map(min c)a)

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Scala, 25 bytes

a=>_.flatMap(a map _.min)

Try it in Scastie!

Pretty trivial solution, but here's a bad explanation anyway:

//a is the first string
a =>_.flatMap(a map _.min)
//b is the second string, x is each char in b
a => b => b.flatMap(x => a map x.min)
//For every char y in a, min(x, y) is chosen
a => b => b.flatMap(x => a.map(y => x.min(y))
//The results are concatenated because flatMap is used
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »