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

Post History

66%
+2 −0
Challenges Substring factor

Haskell, 101 95 bytes import Data.List r a=or$map(a?)$(map read$tail.inits=<<tails(show a))\\[a] a?b=b/=0&&a`mod`b==0 Try it online! Changelog: Saved 4 bytes by replacing an...

posted 4mo ago by Andrew Ray‭  ·  edited 4mo ago by Andrew Ray‭

Answer
#2: Post edited by user avatar Andrew Ray‭ · 2024-07-25T15:31:23Z (4 months ago)
Add description
  • # [Haskell], 101 bytes
  • ``` haskell
  • import Data.List
  • r a=any id$map(a?)$(map read.(tail.inits=<<).tails.show$a)\\[a]
  • a?b=b/=0&&a`mod`b==0
  • ```
  • [Try it online!][TIO-lz0anjqk]
  • [Haskell]: https://www.haskell.org/
  • [TIO-lz0anjqk]: https://tio.run/##NU3BboMwDL37K3xAFRzK0hgSqJr1sF0mbacd12o1A7XRIFQk0rSvZ6HVnuRnPz/5@cL@u@v7ebbDdZwCPnPg/NX6ABOyYfeLtk0Gvqa8z5I0Djh13OZpYNvn1tngzW6X5Yv0ub@MPwlnh8MHH4H3jWkejFit@DSM7akxRswDW4cG2xEw4mt0oXMBd2s8d@HprvzNip/ePjG9Tjb6OU6x0uUzbrf4HuL2jOtHfHEhyzDB3rrO/8fNBdAGigJkSUBawEZSUSpYSFdw4xpIRGjQSyNQWspaQyWrQsdGqiQNtaKKKB4IRVGWRSV0DFBUliJqSVJXqq5B3kF/ "Haskell – Try It Online"
  • # [Haskell], ~~101~~ 95 bytes
  • ``` haskell
  • import Data.List
  • r a=or$map(a?)$(map read$tail.inits=<<tails(show a))\\[a]
  • a?b=b/=0&&a`mod`b==0
  • ```
  • [Try it online!][TIO-lz1e7fo0]
  • [Haskell]: https://www.haskell.org/
  • [TIO-lz1e7fo0]: https://tio.run/##NU1NT8MwDL37V/hQTe1hJYvbJJ0adoALEpw4MsRcWm0R/ZiaSPz8km7iSX7285OfL@x/ur5fFjdcpzngMwfOX50PMCPbaU4GvqZ8yJI0Djh33CaBXZ@70QVv63oVPvWX6Rc5y47HD/4EPjS2ebBis@HTMLWnxlqxDOxGtNhOgBHf0xi6MWC9xXMXnu7K36z45@0L0@vsop/jHCtd/@J@j@8hbs@4fcSXMWQZJti7sfP/cUsBtIOiAFkSkBawk1SUClbSBm5cAYkIDXptBEpLWWkw0hQ6NlIlaagUGaJ4IBRFWRZG6BigqCxF1JKkNqqqQN5Bfw "Haskell – Try It Online"
  • Changelog:
  • * Saved 4 bytes by replacing `any id` with `or`.
  • * Saved 2 bytes by η-expanding `map read.(tail.inits=<<).tails.show$a` to `map read$tail.inits=<<tails(show a)`
  • Explanation:
  • * `tails` returns all suffixes of a given list (note that a string is a list). `inits` returns all prefixes of a given list. By binding (aka `=<<`) the two together, we get all of the contiguous substrings. By also binding `tail`, which drops the first item from a list, we remove the empty string from this list (since `inits` always puts the empty string first in its output). This gives us `\a->tail.inits=<<tails a`
  • * We need to first convert our Int to a string, and convert our list of strings back to a list of ints at the end. We do this by adding `show` and `map read`: `\a->map read$tail.inits=<<tails(show a)`
  • * But we still need to get rid of the input number itself. Since we've already imported Data.List for `inits` and `tails`, we can use the list difference operator `(\\)`, which saves us some bytes over the `remove` function: `(map read$tail.inits=<<tails(show a))\\[a]`
  • * Next we need to check if any of the numbers in the list are factors of the input. For this, we create the `(?)` operator, which checks if its second argument is a factor of its first. Firstly, `0` is not a factor of any number, so we check if the second argument is not zero. (Notice that the "not equal" operator in Haskell is `(/=)`.) Then, we can use the `mod` function to get the modulo and check if that is zero. Iff `b` is not `0` and ```a `mod` b``` is `0`, then `a?b` will return `True`: ```a?b=b/=0&&a`mod`b==0```
  • * Then `map` a partial application of the `(?)` operator over our list of substring numbers: `\a->map(a?)$(map read$tail.inits=<<tails(show a))\\[a]`
  • * Finally, collapse the list into a single boolean output by using the `or` function, which takes a list of booleans and returns `True` only if any member of the list is `True`.
#1: Initial revision by user avatar Andrew Ray‭ · 2024-07-24T20:27:00Z (4 months ago)
# [Haskell], 101 bytes

``` haskell
import Data.List
r a=any id$map(a?)$(map read.(tail.inits=<<).tails.show$a)\\[a]
a?b=b/=0&&a`mod`b==0
```

[Try it online!][TIO-lz0anjqk]

[Haskell]: https://www.haskell.org/
[TIO-lz0anjqk]: https://tio.run/##NU3BboMwDL37K3xAFRzK0hgSqJr1sF0mbacd12o1A7XRIFQk0rSvZ6HVnuRnPz/5@cL@u@v7ebbDdZwCPnPg/NX6ABOyYfeLtk0Gvqa8z5I0Djh13OZpYNvn1tngzW6X5Yv0ub@MPwlnh8MHH4H3jWkejFit@DSM7akxRswDW4cG2xEw4mt0oXMBd2s8d@HprvzNip/ePjG9Tjb6OU6x0uUzbrf4HuL2jOtHfHEhyzDB3rrO/8fNBdAGigJkSUBawEZSUSpYSFdw4xpIRGjQSyNQWspaQyWrQsdGqiQNtaKKKB4IRVGWRSV0DFBUliJqSVJXqq5B3kF/ "Haskell – Try It Online"