Ok :
Code:
~/.../examples/rust >>> ./girlsboys
Girls
("Ashley", [(1991, 43489), (1992, 38451)])
("Emily", [(1996, 25148), (1997, 25729), (1998, 26177), (1999, 26535), (2000, 25953), (2001, 25050), (2002, 24454), (2003, 25682), (2004, 25021), (2005, 23921), (2006, 21381), (2007, 19331)])
("Emma", [(2008, 18787)])
("Isabella", [(2009, 22257), (2010, 22856)])
("Jennifer", [(1970, 46159), (1971, 56785), (1972, 63605), (1973, 62454), (1974, 63106), (1975, 58180), (1976, 59476), (1977, 58961), (1978, 56310), (1979, 56713), (1980, 58383), (1981, 57035), (1982, 57102), (1983, 54335), (1984, 50551)])
("Jessica", [(1985, 48343), (1986, 52657), (1987, 55984), (1988, 51523), (1989, 47890), (1990, 46461), (1993, 34979), (1994, 32113), (1995, 27938)])
("Linda", [(1947, 99685), (1948, 96178), (1949, 90986), (1950, 80437), (1951, 73884), (1952, 67076)])
("Lisa", [(1962, 46085), (1963, 56036), (1964, 54272), (1965, 60262), (1966, 56912), (1967, 52429), (1968, 49528), (1969, 45034)])
("Mary", [(1900, 16708), (1901, 13136), (1902, 14486), (1903, 14275), (1904, 14962), (1905, 16067), (1906, 16370), (1907, 17581), (1908, 18666), (1909, 19258), (1910, 22847), (1911, 24388), (1912, 32298), (1913, 36640), (1914, 45345), (1915, 58187), (1916, 61433), (1917, 64280), (1918, 67372), (1919, 65837), (1920, 70978), (1921, 73983), (1922, 72167), (1923, 71630), (1924, 73513), (1925, 70603), (1926, 67830), (1927, 70617), (1928, 66861), (1929, 63500), (1930, 64129), (1931, 60294), (1932, 59865), (1933, 55484), (1934, 56906), (1935, 55068), (1936, 54361), (1937, 55639), (1938, 56202), (1939, 54902), (1940, 56201), (1941, 58023), (1942, 63242), (1943, 66166), (1944, 62468), (1945, 59284), (1946, 67459), (1953, 64330), (1954, 67994), (1955, 63164), (1956, 61750), (1957, 61092), (1958, 55840), (1959, 54473), (1960, 51474), (1961, 47657)])
("Sophia", [(2011, 21780), (2012, 22158)])
Boys
("David", [(1960, 85928)])
("Jacob", [(1999, 35342), (2000, 34460), (2001, 32520), (2002, 30552), (2003, 29605), (2004, 27868), (2005, 25809), (2006, 24811), (2007, 24238), (2008, 22547), (2009, 21113), (2010, 22052), (2011, 20269), (2012, 18899)])
("James", [(1940, 62452), (1941, 66714), (1942, 77164), (1943, 80236), (1944, 76936), (1945, 74439), (1946, 87432), (1947, 94743), (1948, 88544), (1949, 86776), (1950, 86227), (1951, 87099), (1952, 87035)])
("John", [(1900, 9830), (1901, 6899), (1902, 7908), (1903, 7609), (1904, 8108), (1905, 8059), (1906, 8264), (1907, 8983), (1908, 9342), (1909, 9591), (1910, 11451), (1911, 13446), (1912, 24586), (1913, 29324), (1914, 37945), (1915, 47579), (1916, 50046), (1917, 51854), (1918, 56559), (1919, 53530), (1920, 56918), (1921, 58230), (1922, 57268), (1923, 57469)])
("Michael", [(1954, 88481), (1955, 88279), (1956, 90611), (1957, 92705), (1958, 90494), (1959, 85285), (1961, 86908), (1962, 85038), (1963, 83794), (1964, 82666), (1965, 81058), (1966, 80008), (1967, 82455), (1968, 82027), (1969, 85226), (1970, 85312), (1971, 77601), (1972, 71420), (1973, 67851), (1974, 67579), (1975, 68453), (1976, 66971), (1977, 67608), (1978, 67139), (1979, 67717), (1980, 68656), (1981, 68733), (1982, 68176), (1983, 67976), (1984, 67703), (1985, 64876), (1986, 64170), (1987, 63625), (1988, 64095), (1989, 65386), (1990, 65270), (1991, 60782), (1992, 54373), (1993, 49552), (1994, 44463), (1995, 41403), (1996, 38360), (1997, 37543), (1998, 36608)])
("Robert", [(1924, 60795), (1925, 60903), (1926, 61113), (1927, 61657), (1928, 60665), (1929, 59799), (1930, 62120), (1931, 60495), (1932, 59240), (1933, 54188), (1934, 55823), (1935, 56509), (1936, 58477), (1937, 61807), (1938, 62261), (1939, 59631), (1953, 86092)])
program:
Code:
use std::io;
use std::fs::File;
use std::fs::read_dir;
use std::io::prelude::*;
use std::collections::BTreeMap;
fn main()->io::Result<()>{
let mut maxs_girls:BTreeMap<String,Vec<(i32,i32)>> = BTreeMap::new();
let mut maxs_boys:BTreeMap<String,Vec<(i32,i32)>> = BTreeMap::new();
let mut max:(i32,String);
let mut buf = String::new();
if let Ok(dir) = read_dir("BabyNames") {
for file in dir {
let file = file?;
let mut f = File::open(file.path())?;
buf.clear();
let _ = f.read_to_string(&mut buf);
max = (0,"".to_string());
for line in buf.lines() {
let two:Vec<_> = line.split_whitespace().collect();
if let Ok(number) = two[1].parse::<i32>(){
let name = two[0];
if max.0 < number {
max = (number,name.to_string());
}
}
}
let path = file.path();
let fname = path.to_str().unwrap().to_string();
let it = fname.split('_').collect::<Vec<_>>();
let it1 = it[0].split('/').collect::<Vec<_>>();
let year = it1[1].parse::<i32>().unwrap();
if it[1] == "GirlsNames.txt" {
let r = maxs_girls.entry(max.1).or_insert(Vec::new());
(*r).push((year,max.0));
(*r).sort();
} else {
let r = maxs_boys.entry(max.1).or_insert(Vec::new());
(*r).push((year,max.0));
(*r).sort();
}
}
} else {
println!("Not in directory above BabyNames...");
}
println!("Girls");
for i in maxs_girls {
println!("{:?}",i);
}
println!("Boys");
for i in maxs_boys {
println!("{:?}",i);
}
Ok(())
}