How much output is too much? #1027
shepmaster
started this conversation in
General
Replies: 2 comments 4 replies
-
|
I managed to arrive here |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
I ran a piece of code to make a table of numbers /** iterator yielding 1, 1, 2, 6, 24, ... */
struct Factorial {
acc: usize, n: usize
}
impl Factorial {
fn new() -> Self { Factorial { acc: 1, n: 1 } }
}
impl Iterator for Factorial {
type Item = usize;
fn next(&mut self) -> Option<usize> {
let val = self.acc;
self.acc *= self.n;
self.n += 1;
Some(val)
}
}
fn factorial(x: usize) -> usize {
if x == 0 {
// sorry. it works better this way in these cases
0
} else {
Factorial::new().nth(x).unwrap()
}
}
/** Fn to take a stack and a value and perform a shuffling of the last few elems */
fn ixed_factorial<T: Clone>(mut value: usize, stack: &mut [T]) -> Result<(), String> {
let mut strides: Vec<usize> = Factorial::new().take_while(|&x| x <= value).collect();
// want only the last few elems of the stack
if stack.len() < strides.len() {
Err("Manipulating too many elements on the stack!")?
}
let stride_offset = stack.len() - strides.len();
let mut edit_target = &mut stack[stride_offset..];
let mut swap = edit_target.to_vec();
while let Some(divisor) = strides.pop() {
let index = value / divisor;
value %= divisor;
edit_target[0] = swap.remove(index);
edit_target = &mut edit_target[1..];
}
Ok(())
}
fn main() {
let max_len = 30;
let template = {
let mut tmp = ('a'..=(b'a' + max_len as u8) as char).collect::<Vec<char>>();
tmp.reverse();
tmp
};
println!("<table>");
println!("<tr><th>Code</th><th>Permutation</th></tr>");
for x in 1..=max_len {
let len = template.len();
let slice = &template[len - x..];
println!("<tr><th colspan=\"2\">Permuting <code>{}</code></th></tr>", slice.iter().collect::<String>());
let fact = factorial(x);
let prev_fact = factorial(x-1);
for code in prev_fact..fact {
let mut permed = slice.to_owned();
ixed_factorial(code, &mut permed).unwrap();
println!("<tr><td>{}</td> <td><code>{}</code></td></tr>", code, permed.iter().collect::<String>());
}
}
println!("</table>");
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The playground imposes a hard limit on the amount of stdout / stderr that a given program execution can produce. If your program generates so much output that it was truncated, an error reported from the playground directs you here. If that happens to you and you think that the playground should not have truncated your output, please let us know what you were doing and why you need that much output. This will help us better calibrate the size limits!
Beta Was this translation helpful? Give feedback.
All reactions