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

Assignment #1 #2 #3 #4

Open
wants to merge 6 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
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions assignments/sauryap/week-1/session-2/problem-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello World Saurya");
}
41 changes: 41 additions & 0 deletions assignments/sauryap/week-2/session-1/problem-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fn main() {

let mut arr: [i32; 5] = [5, 6, 3, 1, 10];
let mut j =0;

println!("Before {:?}", arr);
loop {

if arr[j] > arr[j+1]
{
bubbleswap(&mut arr);
j =0;

}else{
j = j+1;
}

if j >=4 {
println!("After {:?}", arr);
break;
}
}


}

fn bubbleswap(arr:&mut [i32; 5]) {
let mut x = 0;
for i in 0..arr.len()-1{

if arr[i] > arr[i+1]
{
println!("{} is great than {}", arr[i], arr[i+1]);
x = arr[i];
arr[i] = arr[i+1];
arr[i+1] = x;
}
}

// println!("{:?}", arr);
}
32 changes: 32 additions & 0 deletions assignments/sauryap/week-2/session-1/problem-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
fn main() {
let word1:&str = "helodi";
let word2:&str = "olehdi";

println!("The strings {} and {} are {} anagrams", word1, word2, checkanagram(word1, word2));
}

fn checkanagram(word1:&str, word2:&str) -> bool {
let mut c = 0;

if word1.len() > word2.len() {
return false
}else {
for i in word1.chars(){
for j in word2.chars(){


if i == j {

c = c+1;
}
}

}
}

if c == word1.len(){
return true
}else {
return false
}
}
26 changes: 26 additions & 0 deletions assignments/sauryap/week-2/session-2/problem-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Program to pass ownership of a struct object, to a method and then returning it.

#[derive(Debug)]
struct Company {
name: String,
ceo: String,
age: i32
}

fn main() {

let c1 = Company {
name: String::from("XYZ"),
ceo: String::from("Xi Ping"),
age: 40
};

let c2 = ownershiptransfer(c1);
println!("{:?}", c2);

}

fn ownershiptransfer(c3:Company) -> Company {
println!("Function call - Ownership Transfer");
return c3;
}
28 changes: 28 additions & 0 deletions assignments/sauryap/week-2/session-2/problem-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Program to pass struct variable as reference and dereferencing to modify value of object

#[derive(Debug)]
struct Company {
name: String,
ceo: String,
age: i32
}

fn main() {

let mut c1 = Company {
name: String::from("XYZ"),
ceo: String::from("Xi Ping"),
age: 40
};
println!("Before Referencing {:?}", c1);

ownershiptransfer(&mut c1);
println!("After Referencing {:?}", c1);

}

fn ownershiptransfer(c1:&mut Company) {
(*c1).name = String::from("ABC");
(*c1).age = 30;

}
203 changes: 203 additions & 0 deletions assignments/sauryap/week-4/session-8/Problem-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
//random guessing game

#[macro_use] extern crate random_number;



fn main() {


loop {
let mut play = String::new();
println!("\n***********How do you want to play***************\n 1. Guess a number\n 2. Guess a range of number\n 3. Guess multiple numbers\n 4. Number higher than\n 5. Number lower than");
println!("Enter your choice: ");
println!("***********");
let mut choice = String::new();
let user_choice = std::io::stdin().read_line(&mut choice).expect("Failed to read from the input");
let trimmed_choice = choice.trim();
let mut res:bool = false;
let n: u8 = random!(1, 100);


match trimmed_choice.parse::<u8>() {
Ok(c) => {
match c {
1 => res = guess_number(n),
2 => res = guess_range(n),
3 => res = guess_multiple(n),
4 => res = guess_higher_than(n),
5 => res = guess_lower_than(n),
_=> println!("Wrong Choice"),
}

if res == true {
println!("Your guess is correct. The number is {}. You win", n);
}
else if res == false && c <= 5 {
println!("Your guess is incorrect. The number is {}. You lose", n);
}
}
Err(..) => println!("this was not an integer: {}", trimmed_choice),
}


println!("Do you want to exit the game? (Y/N)");
let play_choice = std::io::stdin().read_line(&mut play).expect("Failed to read from the input");

let trimmed_play = play.trim();
match trimmed_play.parse::<String>() {
Ok(p) => {
match p.as_str() {
"Y" => { break; },
"y" => { break; },
_=> {continue;},


}

}
Err(..) => println!("this was not a string: {}", trimmed_play),
}

}




}

fn guess_number(n : u8) -> bool {
let mut guess = String::new();
println!("Please type your guess: ");
//random number generator

let user_guess = std::io::stdin().read_line(&mut guess).expect("Failed to read from the input");
let trimmed = guess.trim();
match trimmed.parse::<u8>() {
Ok(i) => {
if i == n {
return true;
}
else {
return false;
}
}
Err(..) => {return false},
};
}

fn guess_range(n : u8) -> bool {
let mut guess1 = String::new();
let mut guess2 = String::new();
println!("Please enter the range of the guess: ");
//random number generator

println!("Please enter the first number: ");
let user_guess1 = std::io::stdin().read_line(&mut guess1).expect("Failed to read from the input");
println!("Please enter the second number: ");
let user_guess2 = std::io::stdin().read_line(&mut guess2).expect("Failed to read from the input");
let trimmed1 = guess1.trim();
let trimmed2 = guess2.trim();
match trimmed1.parse::<u8>() {
Ok(i) => {
match trimmed2.parse::<u8>() {
Ok(j) => {

if (n >= i && n <= j) || (n <= i && n >= j) {
return true;
}
else {
return false;
}

}
Err(..) => return false,

}
}
Err(..) => return false,
}

}

fn guess_multiple(n : u8) -> bool {

let mut number = String::new();
// Vec for storing array values from user
let mut vec = Vec::new();
println!("Numbers you want to guess: ");

let user_selected_number = std::io::stdin().read_line(&mut number).expect("Failed to read from the input");
let trimmed = number.trim();
match trimmed.parse::<u8>() {
Ok(i) => {
println!("Enter the numbers you want: ");
for c in 1..i+1 {
let mut guess = String::new();
let user_guess = std::io::stdin().read_line(&mut guess).expect("Failed to read from the input");

let trimmed1 = guess.trim();
match trimmed1.parse::<u8>() {
Ok(i) => {

vec.push(i);
}
Err(..) => println!("this was not an integer: {}", trimmed1),
}
}

}
Err(..) => {return false},
}
let len = vec.len();

for x in 0..len {

if n == vec[x] {
return true;
}
}
return false;

}

fn guess_higher_than (n : u8) -> bool {
let mut guess = String::new();
println!("Please enter the number: ");
//random number generator
let user_guess1 = std::io::stdin().read_line(&mut guess).expect("Failed to read from the input");
let trimmed = guess.trim();
match trimmed.parse::<u8>() {
Ok(i) => {
if n > i && i <= 100 {
return true;
}
else {
return false;
}
}
Err(..) => {return false}

}

}

fn guess_lower_than (n : u8) -> bool {
let mut guess = String::new();
println!("Please enter the number: ");

let user_guess1 = std::io::stdin().read_line(&mut guess).expect("Failed to read from the input");
let trimmed = guess.trim();
match trimmed.parse::<u8>() {
Ok(i) => {
if n < i {
return true;
}
else {
return false;
}

}
Err(..) => return false,
}
}
Loading