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 »
Sandbox

Post History

66%
+2 −0
Sandbox Multiply two strings [FINALIZED]

posted 3y ago by celtschk‭  ·  edited 1y ago by trichoplax‭

#5: Post edited by user avatar trichoplax‭ · 2023-06-19T10:25:45Z (over 1 year ago)
Add finalized tag now that the sandbox can be filtered to exclude tags
Multiply two strings [FINALIZED]
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"
```

<small>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.</small>
#4: Post edited by user avatar celtschk‭ · 2021-08-24T12:10:57Z (over 3 years ago)
finalized
  • Multiply two strings
  • Multiply two strings [FINALIZED]
#3: Post edited by user avatar celtschk‭ · 2021-08-20T05:06:31Z (over 3 years ago)
Added that reversal of parameters is allowed if explicitly stated
  • 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.
  • 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"
  • ```
  • <small>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.</small>
  • 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"
  • ```
  • <small>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.</small>
#2: Post edited by user avatar celtschk‭ · 2021-08-19T20:14:39Z (over 3 years ago)
  • 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.
  • 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"
  • ```
  • <small>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.</small>
  • 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.
  • 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"
  • ```
  • <small>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.</small>
#1: Initial revision by user avatar celtschk‭ · 2021-08-19T20:13:16Z (over 3 years ago)
Multiply two strings
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.

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"
```

<small>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.</small>