MyriadColors
MyriadColors
Explore posts from servers
MModular
Created by MyriadColors on 5/15/2024 in #questions
How to initialize an empty List[String]
What Im trying to do:
from python import Python

fn add(a: Int, b: Int) -> Int:
return a + b

fn sub(a: Int, b: Int) -> Int:
return a - b

fn mul(a: Int, b: Int) -> Int:
return a * b

fn safe_div(a: Int, b: Int) -> Float32:
return a / b

fn main() raises:
var user_input = Python.evaluate("input('Enter [verb] [args]: ') ").split(" ")

var verb: String = ""
var args: List[String]

for i in range(0, len(user_input)):
if i == 0:
verb = user_input[0]
else:
args.append(user_input[i])
from python import Python

fn add(a: Int, b: Int) -> Int:
return a + b

fn sub(a: Int, b: Int) -> Int:
return a - b

fn mul(a: Int, b: Int) -> Int:
return a * b

fn safe_div(a: Int, b: Int) -> Float32:
return a / b

fn main() raises:
var user_input = Python.evaluate("input('Enter [verb] [args]: ') ").split(" ")

var verb: String = ""
var args: List[String]

for i in range(0, len(user_input)):
if i == 0:
verb = user_input[0]
else:
args.append(user_input[i])
Problem: when compiling: /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:25:24: error: use of uninitialized value 'args' args.append(user_input[i]) ^ /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:5: note: 'args' declared here var args: List[String] ^ mojo: error: failed to run the pass manager But when I try:
var args: List[String] = []
var args: List[String] = []
It also complains: /home/pedrot/Desktop/Coding/mojo/learn/main.mojo:19:30: error: cannot implicitly convert 'ListLiteral[]' value to 'List[String]' in 'var' initializer var args: List[String] = [] ^~ mojo: error: failed to parse the provided Mojo source module THank you.
4 replies
MModular
Created by MyriadColors on 5/15/2024 in #questions
How to take user input form the terminal?
Self-explanatory, the best I could find with research was:
from python import Python

fn main() raises:
var a = Python.evaluate("input('What is your name? ')").to_string()
print(a)
from python import Python

fn main() raises:
var a = Python.evaluate("input('What is your name? ')").to_string()
print(a)
I got this error: What is your name? pedro Unhandled exception caught during execution: 'str' object has no attribute 'to_string' mojo: error: execution exited with a non-zero result: 1 So my questions: 1. How can I do it? Thank you.
31 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
I´ve been writing different sorting algorithms as exercises to learn C# (and algorithms in general). I´ve been testing them on an array of 100 thousand long unique numbers generated by the program every time it is run. The code is as follows: https://paste.mod.gg/guvbnmefzrkd/0 As far as my understanding of algorithms go, my Naive QuickSort and the MergeSort should have the same average time complexity of O(n * log n), I know the worst-case for QuickSort (having a pivot of the biggest value in the array) is extremely unlikely to happen given the dataset (but do correct me if im wrong, please). So i expected both algorithms to have roughly the same performance. However, according to my execution time measurements (you can measure yourself witht he code i´ve provided) the MergeSort code was substantially slower compared to the QuickSort. For example, my quickSort algorithm takes about 40-50ms (in average) to sort the 100k long array, while the Mergesort takes between 1.5 and 2 sconds. Since I am not well-versed in algorithms and how they work, I am at a loss to explain this large of a differential. I believe it is due to the distribution patterns of the random arrays I am creating with my generateUniqueList function, but I dont know what would it be. Thank you for the help.
48 replies