Skip to content

Commit

Permalink
Create: 1472-design-browser-history.rs / .ts / .js / .go
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Jan 12, 2023
1 parent ee7a3bc commit 3e71fc9
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
36 changes: 36 additions & 0 deletions go/1472-design-browser-history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import "math"

func main() {

}

type BrowserHistory struct {
history []string
current int
}

func Constructor(homepage string) BrowserHistory {
return BrowserHistory{
history: []string{homepage},
current: 0,
}
}

func (this *BrowserHistory) Visit(url string) {
this.history = this.history[0: this.current + 1]
this.history = append(this.history, url)
this.current++
}

func (this *BrowserHistory) Back(steps int) string {
this.current = int(math.Max(float64(this.current) - float64(steps), 0))
return this.history[this.current]
}

func (this *BrowserHistory) Forward(steps int) string {
this.current = int(math.Min(float64(this.current) +float64(steps), float64(len(this.history) - 1)))
return this.history[this.current]
}

34 changes: 34 additions & 0 deletions javascript/1472-design-browser-history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class BrowserHistory {
constructor(homepage) {
this.history = [homepage];
this.current = 0;
}

/**
* @param {string} url
* @return {void}
*/

visit(url) {
this.history[++this.current] = url;
this.history.length = this.current + 1;
}

/**
* @param {number} steps
* @return {string}
*/
back(steps) {
this.current = Math.max(this.current - steps, 0);
return this.history[this.current];
}

/**
* @param {number} steps
* @return {string}
*/
forward(steps) {
this.current = Math.min(this.current + steps, this.history.length - 1);
return this.history[this.current];
}
}
28 changes: 28 additions & 0 deletions rust/1472-design-browser-history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct BrowserHistory {
history: Vec<String>,
current: usize,
}

impl BrowserHistory {
fn new(homepage: String) -> Self {
Self {
history: vec![homepage],
current: 0,
}
}

fn visit(&mut self, url: String) {
self.current += 1;
self.history.splice(self.current.., std::iter::once(url));
}

fn back(&mut self, steps: i32) -> String {
self.current = self.current.saturating_sub(steps as usize);
self.history[self.current].clone()
}

fn forward(&mut self, steps: i32) -> String {
self.current = (self.current + steps as usize).min(self.history.len() - 1);
self.history[self.current].clone()
}
}
24 changes: 24 additions & 0 deletions typescript/1472-design-browser-history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class BrowserHistory {
history: string[];
current: number;

constructor(homepage: string) {
this.history = [homepage];
this.current = 0;
}

visit(url: string): void {
this.history[++this.current] = url;
this.history.length = this.current + 1;
}

back(steps: number): string {
this.current = Math.max(this.current - steps, 0);
return this.history[this.current];
}

forward(steps: number): string {
this.current = Math.min(this.current + steps, this.history.length - 1);
return this.history[this.current];
}
}

0 comments on commit 3e71fc9

Please sign in to comment.