i need a python script that outputs rust code to implement bubble sorting for a number…
- User
i need a python script that outputs rust code to implement bubble sorting for a number array
- Assistant
Here is a Python script that generates Rust code for bubble sorting an array of numbers:
def generate_bubble_sort_code(array_name): code = f"fn bubble_sort({array_name}: &mut [i32]) {{\n" code += " let mut n = {array_name}.len();\n" code += " let mut swapped = true;\n" code += " while swapped {\n" code += " swapped = false;\n" code += " for i in 0..n-1 {\n" code += " if {array_name}[i] > {array_name}[i + 1] {\n" code += " {array_name}.swap(i, i + 1);\n" code += " swapped = true;\n" code += " }\n" code += " }\n" code += " n -= 1;\n" code += " }\n" code += "}\n" return code
array_name = "numbers" print(generate_bubble_sort_code(array_name))
This code generates the Rust function bubble_sort that sorts the array numbers. You can change the name of the array by changing the array_name variable.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →