Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week 3, session -2 assignment #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions assignments/rawvieman/bubble_sort/bubble_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn main() {
let unsorted: [i32; 9] = [5, 7, 3, 1, 2, 4, 6, 9, 8];
println!("Unsorted: {:?}", unsorted);
bubble_sort(unsorted);
}
fn bubble_sort(mut arr: [i32; 9]){
for i in 0..arr.len() {
for j in 0..arr.len() - 1 - i {
if arr[j] > arr[j + 1] {
arr.swap(j, j + 1);
}
}
}
println!("Sorted: {:?}", arr);
}
21 changes: 21 additions & 0 deletions assignments/rawvieman/check_anagram/check_anagram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn main() {
check_anagram("abcd", "dbac");
check_anagram("car", "fan");
}
fn check_contains(first: &str, second: &str) -> bool{
let mut contains = true;
for c in first.chars() {
if !second.contains(c){
contains = false;
}
}
return contains;
}
fn check_anagram(first: &str, second: &str){
if check_contains(first, second) && check_contains(second, first){
println!("{} and {} are anagrams", first, second);
}
else{
println!("{} and {} are not anagrams", first, second);
}
}
55 changes: 55 additions & 0 deletions assignments/rawvieman/project/crypto_price_cli/crypto_price_cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[macro_use]
extern crate serde_derive;

#[derive(Debug, Serialize, Deserialize)]
pub struct CMCResponse {
status: Status,
data: Vec<Data>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Data {
id: i64,
symbol: String,
name: String,
quote: Quote,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Quote {
#[serde(rename = "USD")]
usd: Usd,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Usd {
price: f64,
last_updated: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Status {
timestamp: String,
error_code: i64,
error_message: Option<serde_json::Value>,
elapsed: i64,
credit_count: i64,
notice: Option<serde_json::Value>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let res = client.get("https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=10&convert=USD")
.header("X-CMC_PRO_API_KEY", "f4663923-075d-4ec5-b1fc-7d1525d07b4d")
.send()
.await?;
let response : CMCResponse = res.json::<CMCResponse>().await?;
let solana: Vec<&Data> = response
.data
.iter()
.filter(|data| data.symbol == "SOL")
.collect();
println!("Price of solana today is: {}", solana[0].quote.usd.price);
Ok(())
}
112 changes: 112 additions & 0 deletions assignments/rawvieman/project/dice_game/dice_game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use rand::Rng;
fn main() {
println!("1: Enter a number.\n2: Enter multiple numbers.\n3: Enter number between a range.\n4: Higher than a number.\n5: Lower than a number.");
let mut choosed_option = String::new();
println!("Choose an option :");
std::io::stdin().read_line(&mut choosed_option).unwrap();
if choosed_option.trim() == "1" {
let mut line = String::new();
println!("Enter your number :");
std::io::stdin().read_line(&mut line).unwrap();
let trimmed = line.trim();
match trimmed.parse::<u32>() {
Ok(i) => find_match(i),
Err(..) => println!("this was not an number: {}", trimmed),
};
} else if choosed_option.trim() == "2" {
let mut nums: Vec<i32> = Vec::new();
let mut add = true;
while add{
let mut num = String::new();
println!("Enter your number :");
std::io::stdin().read_line(&mut num).unwrap();
nums.push(num.trim().parse::<i32>().unwrap());
let mut c = String::new();
println!("1 to add another number 2 to continue with game:");
std::io::stdin().read_line(&mut c).unwrap();
if c.trim() == "2" {
add = false;
}
}
find_match_between_multiple(nums);
} else if choosed_option.trim() == "3" {
let mut first_num = String::new();
let mut second_num = String::new();
println!("Enter first number :");
std::io::stdin().read_line(&mut first_num).unwrap();
println!("Enter second number :");
std::io::stdin().read_line(&mut second_num).unwrap();
find_match_between_range(first_num, second_num);
} else if choosed_option.trim() == "4" {
let mut num = String::new();
println!("Enter your number :");
std::io::stdin().read_line(&mut num).unwrap();
find_match_higher_than(num);
} else if choosed_option.trim() == "5" {
let mut num = String::new();
println!("Enter your number :");
std::io::stdin().read_line(&mut num).unwrap();
find_match_lower_than(num);
}
}
fn find_match_between_multiple(nums: Vec<i32>){
let rand_num = rand::thread_rng().gen_range(0..100);
println!("Numbers you choosed:");
println!("{:?}", nums);
println!("Number that was predicted: {}", rand_num);
if nums.contains(&i32::from(rand_num)) {
println!("You win");
} else {
println!("You lose")
}
}
fn find_match(num: u32) {
let rand_num = rand::thread_rng().gen_range(0..100);
println!("Your number: {}, predicted number: {}", num, rand_num);
if rand_num == num {
println!("You win");
} else {
println!("You lose")
}
}

fn find_match_between_range(first_num: String, second_num: String) {
let rand_num = rand::thread_rng().gen_range(0..100);
println!(
"Your number was between: {} and {}, predicted number was: {}",
first_num, second_num, rand_num
);
if rand_num >= first_num.trim().parse().unwrap()
&& rand_num <= second_num.trim().parse().unwrap()
{
println!("You win");
} else {
println!("You lose")
}
}

fn find_match_higher_than(num: String) {
let rand_num = rand::thread_rng().gen_range(0..100);
println!(
"Your number was higher than: {}, predicted number: {}",
num, rand_num
);
if rand_num > num.trim().parse().unwrap() {
println!("You win");
} else {
println!("You lose")
}
}

fn find_match_lower_than(num: String) {
let rand_num = rand::thread_rng().gen_range(0..100);
println!(
"Your number was lower than: {}, predicted number: {}",
num, rand_num
);
if rand_num < num.trim().parse().unwrap() {
println!("You win");
} else {
println!("You lose")
}
}
76 changes: 76 additions & 0 deletions assignments/rawvieman/project/quiz_game/quiz_game.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
struct Question {
question: String,
option1: String,
option2: String,
option3: String,
option4: String,
correct_option: u32,
}
fn main() {
let question: [Question; 5] = [
Question {
question: "How are you?".to_string(),
option1: "a".to_string(),
option2: "b".to_string(),
option3: "c".to_string(),
option4: "d".to_string(),
correct_option: 2,
},
Question {
question: "who are you?".to_string(),
option1: "a".to_string(),
option2: "b".to_string(),
option3: "c".to_string(),
option4: "d".to_string(),
correct_option: 3,
},
Question {
question: "Hello hello hello?".to_string(),
option1: "a".to_string(),
option2: "b".to_string(),
option3: "c".to_string(),
option4: "d".to_string(),
correct_option: 1,
},
Question {
question: "Question 4?".to_string(),
option1: "a".to_string(),
option2: "b".to_string(),
option3: "c".to_string(),
option4: "d".to_string(),
correct_option: 2,
},
Question {
question: "Question 5".to_string(),
option1: "a".to_string(),
option2: "b".to_string(),
option3: "c".to_string(),
option4: "d".to_string(),
correct_option: 4,
},
];
let mut score: i32 = 0;
for i in 0..question.len() {
let q = &question[i];
println!(
"{}\nOption A: {}\nOption B: {}\nOption C: {}\nOption D: {}",
q.question, q.option1, q.option2, q.option3, q.option4
);
let mut line = String::new();
println!("Choose an option:");
std::io::stdin().read_line(&mut line).unwrap();
let trimmed = line.trim();
match trimmed.parse::<u32>() {
Ok(i) => {
if i == q.correct_option {
score = score + 1;
println!("Correct Answer\n");
} else {
println!("Incorrect\n");
}
}
Err(..) => println!("this was not an number: {}", trimmed),
};
}
println!("You answered {} question correctly", score);
}
18 changes: 18 additions & 0 deletions assignments/rawvieman/week-3/session-2/problem-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//Program to pass ownership of a struct object, to a method and then returning it.
struct Person {
name: String,
age: i32,
address: String
}
fn main() {
let person = function_call(Person{
name: String::from("abcd"),
age: 18,
address: String::from("abcd"),
});
println!("Name: {}, Age: {}, Address: {}", person.name, person.age, person.address);
}

fn function_call(person: Person) -> Person{
return person;
}
24 changes: 24 additions & 0 deletions assignments/rawvieman/week-3/session-2/problem-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//Program to pass struct variable as reference and dereferencing to modify value of object
struct Person {
name: String,
age: i32,
address: String
}
fn main() {
let mut person = Person {
name: String::from("abcd"),
age: 18,
address: String::from("abcd"),
};
println!("Name: {}, Age: {}, Address: {}", person.name, person.age, person.address);
function_call(&mut person);
println!("Name: {}, Age: {}, Address: {}", person.name, person.age, person.address);
}

fn function_call(person: &mut Person){
*person = Person{
name: person.name.clone(),
age: 55,
address: String::from("ddddd")
};
}
Binary file added output.pdb
Binary file not shown.