Upload files to "/"
This commit is contained in:
30
BinarySearch.ts
Normal file
30
BinarySearch.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
|
||||||
|
// Searching algorithm: Binary search
|
||||||
|
// Write function bSearch so that it return the location of target if
|
||||||
|
// it is found in array list. If target is not found bSearch returns -1
|
||||||
|
function bSearch(list: string[], target: string) {
|
||||||
|
let left = 0
|
||||||
|
let right = list.length - 1
|
||||||
|
|
||||||
|
while (left <= right) {
|
||||||
|
let mid = Math.floor((left + right) / 2)
|
||||||
|
if (list[mid] === target) {
|
||||||
|
return mid
|
||||||
|
}
|
||||||
|
if (list[mid]! < target) {
|
||||||
|
left = mid + 1
|
||||||
|
} else {
|
||||||
|
right = mid - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
// use the following array to test your function
|
||||||
|
let arr = ["Allegator", "Bee", "Bull", "Dog", "Ferret", "Tiger", "Zebra"] // already sorted a-z
|
||||||
|
|
||||||
|
console.log("Zebra was at location " + bSearch(arr, "Zebra"))
|
||||||
|
console.log("Bee was at location " + bSearch(arr, "Bee"))
|
||||||
|
console.log("Allegator was at location " + bSearch(arr, "Allegator"))
|
||||||
82
LabArrays2.ts
Normal file
82
LabArrays2.ts
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
|
||||||
|
// humberto mundo
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Objective: shift() and push()
|
||||||
|
// Write a function named circulate(list). This function takes an array as parameter and
|
||||||
|
// returns the same array but the elements move up one spot. For example, say the
|
||||||
|
// array is ["Zebra", "Koala", "Lion", "Elephan", "Eagle", "Cat", "Eagle"], circulate will
|
||||||
|
// return ["Eagle", "Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat"]
|
||||||
|
let array1: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
||||||
|
|
||||||
|
function circulate(list: string[]) {
|
||||||
|
if (list.length === 0 ) return []; // since if length of the arry is 0 return not
|
||||||
|
const lastElement = list.pop()!;
|
||||||
|
list.unshift(lastElement);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
// Objective: slice(1, 5)
|
||||||
|
// Write a function named getHalf(). This function takes an array as a parameter and copies
|
||||||
|
// the first half of an array and returns it. if the the array contains odd number of elements,
|
||||||
|
// the middle element should belong to the first half.
|
||||||
|
// For example ["Zebra", "Koala", "Lion", "Elephan", "Eagle", "Cat", "Eagle"], this function
|
||||||
|
// return ["Zebra", "Koala", "Lion", "Elephant"]
|
||||||
|
|
||||||
|
let array2: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"];
|
||||||
|
|
||||||
|
function getHalf(list: string[]) {
|
||||||
|
return list.slice(0, Math.ceil(list.length / 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
console.log()
|
||||||
|
|
||||||
|
// Write a function named removeDuplicates(). This function takes an array as a parameter, removes,
|
||||||
|
// all duplicates and returns the new array (without duplicates)
|
||||||
|
// For example ["Lion", "Koala", "Lion", "Elephan", "Eagle", "Cat", "CAT"], this function
|
||||||
|
// return ["Lion", "Koala", "Elephant", "Eagle", "Cat"],
|
||||||
|
|
||||||
|
let array3: string[] = ["Lion", "Koala", "Lion", "Elephant", "Eagle", "Cat", "CAT"]
|
||||||
|
|
||||||
|
function removeDuplicates(list: string[]) {
|
||||||
|
let seen = new Set<string>();
|
||||||
|
// let newarray: string[] = [];
|
||||||
|
for (let i = 0; i < list.length; i++) {
|
||||||
|
let item: string = list[i]!
|
||||||
|
// console.log(!seen.has(item))
|
||||||
|
if(!seen.has(item.toLowerCase())) {
|
||||||
|
seen.add(item.toLowerCase())
|
||||||
|
list.splice(list.indexOf(item), list.indexOf(item))
|
||||||
|
// console.log(`added ${item.toLowerCase()}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(removeDuplicates(array3))
|
||||||
|
// want [ 'Lion', 'Koala', 'Elephant', 'Eagle', 'Cat' ]
|
||||||
|
|
||||||
|
console.log()
|
||||||
|
console.log()
|
||||||
|
// Write a function named sortNames(). This function takes an array as a parameter,
|
||||||
|
// and returns a new array but with names sorted in ascending order
|
||||||
|
// For example say list = ["Lion", "Cat", "Zebra", "Eagle", "Elephan", "Koala"], the
|
||||||
|
// folowing call sortNames(list) will return
|
||||||
|
// ["Cat", "Eagle", "Elephan", "Koala", "Lion", "Zebra"]
|
||||||
|
|
||||||
|
let array4: string[] = ["Lion", "Cat", "Zebra", "Eagle", "Elephan", "Koala"]
|
||||||
|
|
||||||
|
function sortNames(list: string[]) {
|
||||||
|
return list.sort()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
67
meow.ts
Normal file
67
meow.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
// humberto mundo
|
||||||
|
let animals: string[] = ["Zebra", "Koala", "Lion", "Elephant", "Eagle", "Cat", "Eagle"]
|
||||||
|
|
||||||
|
// Write a function named getLocation(list, word) to return the location of the parameter word
|
||||||
|
// in the array list, if it exist, returns -1 if it does not exists
|
||||||
|
|
||||||
|
function getLocation(list: string[], word: string): number {
|
||||||
|
return list.indexOf(word)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Write a function named getLongest(list) which needs to return the longest string in array
|
||||||
|
// parameter array
|
||||||
|
|
||||||
|
function getLongest(array: string[] ): string {
|
||||||
|
if (!array) return "provide a array";
|
||||||
|
|
||||||
|
let long = array[0]; // init w/ first element
|
||||||
|
for (let i = 1; i < array.length; i++) {
|
||||||
|
// const element = list[i];
|
||||||
|
if (array[i]!.length > long!.length) { // this is why i love and hate typescript :3
|
||||||
|
long = array[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return long as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// extra cus y not
|
||||||
|
|
||||||
|
function getShortest(list: string[]): string {
|
||||||
|
if (!list) return "i need array >:("
|
||||||
|
|
||||||
|
let short = list[0]
|
||||||
|
|
||||||
|
for (let i = 1; i < list.length; i++) {
|
||||||
|
if (list[i]!.length < short!.length) {
|
||||||
|
short = list[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return short as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Write a function named spellBackward(list, word) which it needs to return parameter word
|
||||||
|
// spell backwards if it exist in array list, otherwise it return word does not exist.
|
||||||
|
|
||||||
|
function spellBackward(list: string[], word: string): string {
|
||||||
|
if (!list.includes(word)) return "word does not exist";
|
||||||
|
return word.split("").reverse().join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//write the rest of the code here
|
||||||
|
|
||||||
|
console.log(getLocation(animals, "Eagle")) // uhhhh 4
|
||||||
|
console.log(getLocation(animals, "cat")) // should return -1 since its Cat not cat
|
||||||
|
|
||||||
|
console.log() // spacers
|
||||||
|
|
||||||
|
console.log(getLongest(animals)) // Elephant
|
||||||
|
console.log(getShortest(animals)) // Cat
|
||||||
|
|
||||||
|
console.log() // spacers
|
||||||
|
|
||||||
|
// i am the best programmer in the world
|
||||||
|
console.log(spellBackward(animals, "Eagle")) // should return elgaE
|
||||||
|
console.log(spellBackward(animals, "cat")) // should return "word does not exist"
|
||||||
61
mewhenimbored.ts
Normal file
61
mewhenimbored.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
enum CarType {
|
||||||
|
Sedan = 'Sedan',
|
||||||
|
SUV = 'SUV',
|
||||||
|
Hatchback = 'Hatchback',
|
||||||
|
Truck = 'Truck',
|
||||||
|
Coupe = 'Coupe',
|
||||||
|
}
|
||||||
|
|
||||||
|
enum RunsOn {
|
||||||
|
Gasoline = 'Gasoline',
|
||||||
|
Diesel = 'Diesel',
|
||||||
|
Hybrid = 'Hybrid',
|
||||||
|
Electric = 'Electric',
|
||||||
|
}
|
||||||
|
|
||||||
|
// interface ICar {
|
||||||
|
// make: string;
|
||||||
|
// model: string;
|
||||||
|
// year: number;
|
||||||
|
// body: CarType;
|
||||||
|
// powertrain: RunsOn;
|
||||||
|
// engine: string;
|
||||||
|
// mpg: number;
|
||||||
|
// link?: string;
|
||||||
|
// }
|
||||||
|
|
||||||
|
class Car {
|
||||||
|
constructor(
|
||||||
|
public make: string,
|
||||||
|
public model: string,
|
||||||
|
public year: number,
|
||||||
|
public body: CarType,
|
||||||
|
public powertrain: RunsOn,
|
||||||
|
public engine: string,
|
||||||
|
public mpg: number,
|
||||||
|
public link?: string
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public info(): string {
|
||||||
|
// return `${this.year} ${this.make} ${this.model} (${this.body}) - Engine: ${this.engine}, Powertrain: ${this.powertrain}, MPG: ${this.mpg} \nAnd Visit ${this.link} for more infomation`;
|
||||||
|
let result = `${this.year} ${this.make} ${this.model} (${this.body}) - Engine: ${this.engine}, Powertrain: ${this.powertrain}, MPG: ${this.mpg}`;
|
||||||
|
if (this.link) {
|
||||||
|
result += `\nAnd Visit ${this.link} for more information`;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const myCar = new Car(
|
||||||
|
'Honda', // Make
|
||||||
|
'Civic', // Model
|
||||||
|
2025, // Year
|
||||||
|
CarType.Hatchback, // Body
|
||||||
|
RunsOn.Gasoline, // PowerTrain
|
||||||
|
'2L 4-Cylinder', // Engine
|
||||||
|
49,
|
||||||
|
"https://automobiles.honda.com/2024/civic-sedan/specs-features-trim-comparison"
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log(myCar.info());
|
||||||
46
problem1.ts
Normal file
46
problem1.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
|
||||||
|
// Function computeAverage() is complete. This function
|
||||||
|
// takes an array of test named scores and two integers, from and to.
|
||||||
|
// computeScores returns the average of all the scores starting at
|
||||||
|
// from and ending at to, inclusive
|
||||||
|
function computeScores(scores:number[], from: number, to:number): number {
|
||||||
|
//DO NOT WRITE ANY CODE HERE
|
||||||
|
let sum = 0;
|
||||||
|
let average =0;
|
||||||
|
for(let i=from; i<=to; i++)
|
||||||
|
{
|
||||||
|
sum = sum + scores[i]!
|
||||||
|
}
|
||||||
|
|
||||||
|
average = sum/(to-from+1)
|
||||||
|
return average
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Function getRange() is complete. This function
|
||||||
|
// takes an array of test scores and two integers, from and to.
|
||||||
|
// getRange returns a string consisting of all the scores from scores
|
||||||
|
// starting at from and ending at to, inclusive separated by a space
|
||||||
|
function getRange(scores: number[], from: number, to:number): string {
|
||||||
|
//DO NOT WRITE ANY CODE HERE
|
||||||
|
let s="";
|
||||||
|
for(let i=from; i<=to; i++)
|
||||||
|
{
|
||||||
|
s = s + scores[i] +" "
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// ======= Your task is specified in the WORD DOCUMENT ================
|
||||||
|
|
||||||
|
console.log(`Scores: ${getRange([95, 100,80, 80, 100, 80, 60, 80, 90], 2, 5)}`)
|
||||||
|
console.log(`Average:${computeScores([95, 100,80, 80, 100, 80, 60, 80, 90], 2, 5)}`)
|
||||||
|
console.log()
|
||||||
|
console.log(`Scores: ${getRange( [95, 60, 80, 80, 50, 80, 60, 80, 90], 4, 8)}`)
|
||||||
|
console.log(`Average:${computeScores( [95, 60, 80, 80, 50, 80, 60, 80, 90], 4, 8)}`)
|
||||||
|
console.log()
|
||||||
|
console.log(`Scores: ${getRange([95, 100, 75, 80, 80, 80, 60, 80, 75], 0, 8)}`)
|
||||||
|
console.log(`Average:${computeScores([95, 100, 75, 80, 80, 80, 60, 80, 75], 0, 8)}`)
|
||||||
Reference in New Issue
Block a user