Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashMap;
- use std::{fs, io::{self, Write}};
- fn main() -> io::Result<()> {
- let input = fs::read_to_string("sygnaly.txt")?;
- let input = input.lines().collect::<Vec<_>>();
- let mut output = fs::File::create("wyniki4.txt")?;
- output.write_all(excersise_1(&input).as_bytes())?;
- output.write_all(b"\n")?;
- output.write_all(excersise_2(&input).as_bytes())?;
- output.write_all(b"\n")?;
- for line in excersise_3(&input) {
- output.write_all(line.as_bytes())?;
- output.write_all(b"\n")?;
- }
- Ok(())
- }
- fn excersise_1(input: &Vec<&str>) -> String {
- input
- .iter()
- .skip(39)
- .step_by(40)
- .map(|line| line.chars().nth(9).unwrap())
- .collect::<String>()
- }
- fn excersise_2(input: &Vec<&str>) -> String {
- let mut letters = HashMap::with_capacity(26);
- let mut max = (0, "");
- for line in input {
- letters.clear();
- for c in line.chars() {
- letters.entry(c).or_insert(());
- }
- if letters.len() > max.0 {
- max = (letters.len(), line);
- }
- }
- format!("{} {}", max.1, max.0)
- }
- fn excersise_3<'a>(input: &'a Vec<&str>) -> Vec<&'a str> {
- input.iter().filter(|line| {
- let bytes = line.as_bytes();
- for i in 0..(bytes.len() - 1) {
- for j in 1..bytes.len() {
- if (bytes[i] as i16 - bytes[j] as i16).abs() > 10 {
- return false;
- }
- }
- }
- true
- })
- .map(|line| *line)
- .collect::<Vec<_>>()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement