This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Тип, по которому строится битовый итератор | |
type BitBase = u64; | |
//Чтобы избежать случайной модификации, | |
//выносим итератор в отдельный модуль | |
mod bit_iter { | |
//Импортируем тип из вышележащего модуля | |
use super::BitBase; | |
//Структура итератора по битам |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tribonacci = 0:0:1:zipWith3 (\x y z -> x+y+z) tribonacci (tail tribonacci) (drop 2 tribonacci) | |
solutions = 0:drop 4 tribonacci | |
solution = (solutions !!) | |
main = print . solution $ 4 -- 13 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Тип входных данных | |
type Base = u32; | |
//Возвращает НОД аргументов. | |
//Если один аргумент нулевой, возвращает другой | |
fn gcd(a: Base, b: Base) -> Base { | |
if a < b { gcd(b, a) } | |
else if b == 0 { a } | |
else { gcd(b, a % b) } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, PartialEq)] | |
enum TumblerError { //Тип ошибки. | |
StepIsTooLarge, //Номер шага больше MAX_BOUND | |
ZeroStep, //Номер шага равен нулю (нулевого тумблера нет) | |
} | |
type Step = u32; | |
type FloatBase = f64; | |
#[deny(overflowing_literals)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Number = u32; //Тип элементов массива | |
#[deny(overflowing_literals)] | |
const MAX_BOUND: Number = 10_000; //Ограничение на входные данные | |
#[derive(Debug, PartialEq)] | |
enum LeastNotInSeqErr { //Тип ошибок при решении задачи | |
OutOfBoundAt(usize), //Элемент с указанным индексом больше MAX_BOUND | |
ContainsZeroAt(usize), //Элемент с указанным индексом равен нулю | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Float = f64; //Тип значения площади | |
type Side = u32; //Тип стороны | |
//Проверяет, удолетворяет ли тройка значений неравенствам треугольника | |
fn valid_triangle (a: Side, b: Side, c: Side) -> bool { | |
a + b > c && | |
b + c > a && | |
c + a > b | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn digital_root(n: u32) -> u32 { | |
if n == 0 { return 0; } | |
let modulus = n % 9; | |
if modulus == 0 { 9 } else { modulus } | |
} | |
fn main() { | |
#![deny(overflowing_literals)] | |
assert_eq!(digital_root(65536), 7); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Bar = u32; //Тип столбцов гистограммы | |
type RectArea = u64; //Тип площади прямоугольников | |
fn max_rect(bar_chart: &[Bar]) -> RectArea { | |
use std::collections::BTreeSet; | |
use std::iter::FromIterator; | |
//Собираем все высоты, присутствующие в гистограмме | |
let heights = BTreeSet::from_iter(bar_chart.iter().cloned()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Bar = u32; //Тип столбца гистограммы | |
//Вспомогательная функция -- вывод гистограммы на печать | |
fn print_bar_chart(chart: &[Bar]) { | |
//Находим максимальную высоту столбцов | |
let max_height = chart.iter().map(|&x| x).fold(0, |acc, x| acc.max(x)); | |
//Для каждого значения от максимальной высоты до 1 включительно | |
for height in (0..max_height).map(|x| x + 1).rev() { | |
//и каждой колонки гистограммы |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Включить использование паттернов для слайсов. | |
//На текущий момент (20 марта 2018) эта фича доступна | |
//только на ночной версии компилятора | |
#![feature(slice_patterns)] | |
//Тип элементов в проверяемом массиве | |
type Elem = i64; | |
//Проверяет, находятся ли все элементы arr в одном и том же | |
//отношении порядка (т. е. все больше, все меньше или все равны) |