Created
November 18, 2022 11:30
-
-
Save ujjwal-kr/59234e996b2102e32effc8659cf796af to your computer and use it in GitHub Desktop.
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
| #include <iostream> | |
| #include <unordered_map> | |
| #include <string> | |
| #include <algorithm> | |
| void get_label(std::unordered_map<std::string, size_t> map, int pc) { | |
| std::vector<std::size_t> label_point_vec = {}; | |
| std::vector<std::string> key_vec = {}; | |
| for (const auto &pair : map) { | |
| label_point_vec.push_back(pair.second); | |
| key_vec.push_back(pair.first); | |
| } | |
| std::sort(label_point_vec.begin(), label_point_vec.end()); | |
| size_t final_point; | |
| std::string final_label; | |
| std::vector<size_t> point_stack; | |
| for (auto point : label_point_vec) { | |
| if (pc >= point) { | |
| point_stack.push_back(point); | |
| } | |
| } | |
| final_point = point_stack[point_stack.size() - 1]; | |
| for (auto& k : key_vec) { | |
| auto n = map.find(k); | |
| if (n->second == final_point) { | |
| final_label = k; | |
| break; | |
| } | |
| } | |
| std::cout << final_label << std::endl; | |
| } | |
| int main() { | |
| std::unordered_map<std::string, size_t> map; | |
| int pc = 17; | |
| map["main"] = 0; | |
| map["kek"] = 5; | |
| map["ok"] = 17; | |
| get_label(map, pc); | |
| } | |
use std::io;
fn main() {
let mappings = vec![vec!["main", "0"], vec!["test", "7"], vec!["kek", "15"]];
let mut input = String::new();
println!("Enter A Number: ");
io::stdin()
.read_line(&mut input)
.expect("An error occured while reading input");
let input = input
.trim()
.parse::<i32>()
.expect("Invalid Number Supplied");
let size = mappings.len() - 1;
for (key, value) in mappings.iter().enumerate() {
if (input >= mappings[mappings.len() - 1][1].parse::<i32>().unwrap()) {
return println!("{}", mappings[mappings.len() - 1][0]);
}
if (input >= value[1].parse::<i32>().unwrap()
&& input < mappings[key + 1][1].parse::<i32>().unwrap())
{
return println!("{}", mappings[key][0]);
}
}
}Took me almost an hour but I think it was worth it.
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.