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

75%
+4 −0
Challenges Versatile self-printer

Scala 3 and Python 3.8 (pre-release), 2 languages (385 bytes) def String():Any=0 def Int():Any=0 def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3)) class M: f("""def Strin...

posted 3y ago by user‭  ·  edited 3y ago by General Sebast1an‭

Answer
#3: Post edited by user avatar General Sebast1an‭ · 2021-08-12T15:00:14Z (about 3 years ago)
  • # [Scala 3](https://docs.scala-lang.org/scala3/) and [Python 3.8 (pre-release)], 385 bytes
  • <!-- language-all: lang-python -->
  • def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f("""def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f(Q)
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()""")
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()
  • [Try it online in Scala!](https://scastie.scala-lang.org/UTPHA7eVRDmuWj7oIeSmpw)
  • [Try it online in Python!][TIO-kqmo663r]
  • [Python 3.8 (pre-release)]: https://docs.python.org/3.8/
  • [TIO-kqmo663r]: https://tio.run/##xU89C8IwEN3zK0qmOytFqUMJBBQnhwziLzhqawttGpMs/fUxaUVwF5zu3eN98Mzsu0mXlbEh3Js2u3nb6wegOOlZ7liiLtp//S04scpW1kTowRW2MQPVDdSdhWqP23TLA27K3OUfjMjqgZzLlGBZjOKc/6P2ikvmSL0GEgqFkrQwyUEiduO7THKeU@Gnc0eWHZNh0T0heRRgHPCzrBBe "Python 3.8 (pre-release) – Try It Online"
  • `def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))`
  • `f` is where everything happens. In Scala, the `print` method doesn't add a newline, but that doesn't matter to us. Both Scala and Python have a `replace` method on strings and can multiply strings by numbers, but they have different ways of turning integers into strings, which is why the `chr` method is defined later for Scala's benefit. It's already present in Python, but the interpreter sees it after `f`, and it's never run anyway. `s` is a string literal which contains basically the entire program, but the definition of `s` is replaced with `Q`. In `f`, this `Q` is replaced with `"""<contents of s>"""` to mirror the definition of `s`, and that gets printed.
  • While Python can have expressions outside functions and classes, Scala needs a main method. Thus, the call to `f` is put inside a `class M`. Python runs this when the class loads, while Scala calls it in the method `q`, when the constructor is called using `M()`. However, to make the `@main` annotation to work with Python, a function named `main` was made.
  • `chr` is already defined in Python, and to make it work in Scala, the `chr` method converts an integer to a string based on its ASCII value.
  • `Int` and `String` are Scala builtins that we need to annotate the parameters to `f` and `chr` with, but Python doesn't know about them, which is why `def String()` and `def Int()` are defined. I made them functions instead of plain variables because Scala requires variables to be declared with `val` and `var`, something that is not possible in Python. While Scala sees `def String` as a method that takes nothing and can output `Any` value (specifically, `0`), Python sees it as setting the value of a variable `Any` to `0`. Neither of these two have any real impact on the code other than making it work nicely with Python.
  • # [Scala 3](https://docs.scala-lang.org/scala3/) and [Python 3.8 (pre-release)], 2 languages (385 bytes)
  • <!-- language-all: lang-python -->
  • def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f("""def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f(Q)
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()""")
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()
  • [Try it online in Scala!](https://scastie.scala-lang.org/UTPHA7eVRDmuWj7oIeSmpw)
  • [Try it online in Python!][TIO-kqmo663r]
  • [Python 3.8 (pre-release)]: https://docs.python.org/3.8/
  • [TIO-kqmo663r]: https://tio.run/##xU89C8IwEN3zK0qmOytFqUMJBBQnhwziLzhqawttGpMs/fUxaUVwF5zu3eN98Mzsu0mXlbEh3Js2u3nb6wegOOlZ7liiLtp//S04scpW1kTowRW2MQPVDdSdhWqP23TLA27K3OUfjMjqgZzLlGBZjOKc/6P2ikvmSL0GEgqFkrQwyUEiduO7THKeU@Gnc0eWHZNh0T0heRRgHPCzrBBe "Python 3.8 (pre-release) – Try It Online"
  • `def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))`
  • `f` is where everything happens. In Scala, the `print` method doesn't add a newline, but that doesn't matter to us. Both Scala and Python have a `replace` method on strings and can multiply strings by numbers, but they have different ways of turning integers into strings, which is why the `chr` method is defined later for Scala's benefit. It's already present in Python, but the interpreter sees it after `f`, and it's never run anyway. `s` is a string literal which contains basically the entire program, but the definition of `s` is replaced with `Q`. In `f`, this `Q` is replaced with `"""<contents of s>"""` to mirror the definition of `s`, and that gets printed.
  • While Python can have expressions outside functions and classes, Scala needs a main method. Thus, the call to `f` is put inside a `class M`. Python runs this when the class loads, while Scala calls it in the method `q`, when the constructor is called using `M()`. However, to make the `@main` annotation to work with Python, a function named `main` was made.
  • `chr` is already defined in Python, and to make it work in Scala, the `chr` method converts an integer to a string based on its ASCII value.
  • `Int` and `String` are Scala builtins that we need to annotate the parameters to `f` and `chr` with, but Python doesn't know about them, which is why `def String()` and `def Int()` are defined. I made them functions instead of plain variables because Scala requires variables to be declared with `val` and `var`, something that is not possible in Python. While Scala sees `def String` as a method that takes nothing and can output `Any` value (specifically, `0`), Python sees it as setting the value of a variable `Any` to `0`. Neither of these two have any real impact on the code other than making it work nicely with Python.
#2: Post edited by user avatar user‭ · 2021-07-02T18:50:55Z (over 3 years ago)
  • # [Scala 3](https://docs.scala-lang.org/scala3/) and [Python 3.8 (pre-release)], 385 bytes
  • <!-- language-all: lang-python -->
  • def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f("""def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f(Q)
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()""")
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()
  • [Try it online in Scala!](https://scastie.scala-lang.org/UTPHA7eVRDmuWj7oIeSmpw)
  • [Try it online in Python!][TIO-kqmo663r]
  • [Python 3.8 (pre-release)]: https://docs.python.org/3.8/
  • [TIO-kqmo663r]: https://tio.run/##xU89C8IwEN3zK0qmOytFqUMJBBQnhwziLzhqawttGpMs/fUxaUVwF5zu3eN98Mzsu0mXlbEh3Js2u3nb6wegOOlZ7liiLtp//S04scpW1kTowRW2MQPVDdSdhWqP23TLA27K3OUfjMjqgZzLlGBZjOKc/6P2ikvmSL0GEgqFkrQwyUEiduO7THKeU@Gnc0eWHZNh0T0heRRgHPCzrBBe "Python 3.8 (pre-release) – Try It Online"
  • Explanation coming soon.
  • # [Scala 3](https://docs.scala-lang.org/scala3/) and [Python 3.8 (pre-release)], 385 bytes
  • <!-- language-all: lang-python -->
  • def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f("""def String():Any=0
  • def Int():Any=0
  • def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
  • class M:
  • f(Q)
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()""")
  • def main(a:M):M=a
  • def chr(a:Int):String=""+a.toChar
  • @main
  • def q():M=M()
  • [Try it online in Scala!](https://scastie.scala-lang.org/UTPHA7eVRDmuWj7oIeSmpw)
  • [Try it online in Python!][TIO-kqmo663r]
  • [Python 3.8 (pre-release)]: https://docs.python.org/3.8/
  • [TIO-kqmo663r]: https://tio.run/##xU89C8IwEN3zK0qmOytFqUMJBBQnhwziLzhqawttGpMs/fUxaUVwF5zu3eN98Mzsu0mXlbEh3Js2u3nb6wegOOlZ7liiLtp//S04scpW1kTowRW2MQPVDdSdhWqP23TLA27K3OUfjMjqgZzLlGBZjOKc/6P2ikvmSL0GEgqFkrQwyUEiduO7THKeU@Gnc0eWHZNh0T0heRRgHPCzrBBe "Python 3.8 (pre-release) – Try It Online"
  • `def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))`
  • `f` is where everything happens. In Scala, the `print` method doesn't add a newline, but that doesn't matter to us. Both Scala and Python have a `replace` method on strings and can multiply strings by numbers, but they have different ways of turning integers into strings, which is why the `chr` method is defined later for Scala's benefit. It's already present in Python, but the interpreter sees it after `f`, and it's never run anyway. `s` is a string literal which contains basically the entire program, but the definition of `s` is replaced with `Q`. In `f`, this `Q` is replaced with `"""<contents of s>"""` to mirror the definition of `s`, and that gets printed.
  • While Python can have expressions outside functions and classes, Scala needs a main method. Thus, the call to `f` is put inside a `class M`. Python runs this when the class loads, while Scala calls it in the method `q`, when the constructor is called using `M()`. However, to make the `@main` annotation to work with Python, a function named `main` was made.
  • `chr` is already defined in Python, and to make it work in Scala, the `chr` method converts an integer to a string based on its ASCII value.
  • `Int` and `String` are Scala builtins that we need to annotate the parameters to `f` and `chr` with, but Python doesn't know about them, which is why `def String()` and `def Int()` are defined. I made them functions instead of plain variables because Scala requires variables to be declared with `val` and `var`, something that is not possible in Python. While Scala sees `def String` as a method that takes nothing and can output `Any` value (specifically, `0`), Python sees it as setting the value of a variable `Any` to `0`. Neither of these two have any real impact on the code other than making it work nicely with Python.
#1: Initial revision by user avatar user‭ · 2021-07-02T18:30:15Z (over 3 years ago)
# [Scala 3](https://docs.scala-lang.org/scala3/) and [Python 3.8 (pre-release)], 385 bytes

<!-- language-all: lang-python -->

    def String():Any=0
    def Int():Any=0
    def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
    class M:
      f("""def String():Any=0
    def Int():Any=0
    def f(s:String):Any=print(s.replace(chr(81),chr(34)*3+s+chr(34)*3))
    class M:
      f(Q)
    def main(a:M):M=a
    def chr(a:Int):String=""+a.toChar
    @main
    def q():M=M()""")
    def main(a:M):M=a
    def chr(a:Int):String=""+a.toChar
    @main
    def q():M=M()

[Try it online in Scala!](https://scastie.scala-lang.org/UTPHA7eVRDmuWj7oIeSmpw)

[Try it online in Python!][TIO-kqmo663r]

[Python 3.8 (pre-release)]: https://docs.python.org/3.8/
[TIO-kqmo663r]: https://tio.run/##xU89C8IwEN3zK0qmOytFqUMJBBQnhwziLzhqawttGpMs/fUxaUVwF5zu3eN98Mzsu0mXlbEh3Js2u3nb6wegOOlZ7liiLtp//S04scpW1kTowRW2MQPVDdSdhWqP23TLA27K3OUfjMjqgZzLlGBZjOKc/6P2ikvmSL0GEgqFkrQwyUEiduO7THKeU@Gnc0eWHZNh0T0heRRgHPCzrBBe "Python 3.8 (pre-release) – Try It Online"

Explanation coming soon.