Apply Function to Each Array Element in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:28:17

155 Views

ProblemSuppose, a mathematical function given by −f(x) = ax2 + bx + cWhere a, b and c are three constants.We are required to write a JavaScript function that takes in a sorted array of Integers, arr as the first argument and a, b and c as second, third and fourth argument. The function should apply the function f(x) to each element of the array arr.And the function should return a sorted version of the transformed array.For example, if the input to the function is −const arr = [-8, -3, -1, 5, 7, 9]; const a = 1; const b = ... Read More

Placing Same String Characters Apart in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:25:32

166 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first argument and a number, num, (num {    const map = {};    for(let i=0; i{       if(map[a] len){       return "";    };    const res = [];    let index = 0, max = str.length-1;    while(keys.length){       let currKey = keys.shift();       let count = map[currKey];       while(count){          res[index] = currKey;          index = index+2;          if(index>max)             index=1;             count--;       }    }    return res.join(""); }; console.log(placeApart(str));OutputAnd the output in the console will be −mlmklk

Counting N-Digit Numbers with All Unique Digits in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:23:20

297 Views

ProblemWe are required to write a JavaScript function that takes in a number, let’s say num, as the only argument. The function should count all such numbers that have num digits and all of their digits are unique.For example, if the input to the function is −const num = 1;Then the output should be −const output = 10;Output Explanation:The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all have 1 digit and all are unique.ExampleThe code for this will be − Live Democonst num = 1; const uniqueDigits = (num = 1) => {    const dp = [1, 10];    const sum = [1, 11];    for (let i = 2; i

Calculate Absolute and Scale of a Vertex in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:27:19

280 Views

ExampleAbs(x, y) => √(x)2+(y)2Scale(f) => (x*f, y*f)ApproachDefine a vertex as a struct.Initialize the vertex with some x and value.Define a method to calculate absolute(x, y).Define a method to calculate scale(x*f, y*f).Example Live Demopackage main import (    "fmt"    "math" ) type Vertex struct {    X, Y float64 } func Abs(v Vertex) float64{    return math.Sqrt(v.X*v.X + v.Y*v.Y) } func Scale(v *Vertex, f float64) {    v.X = v.X * f    v.Y = v.Y * f } func main() {    v := Vertex{3, 4}    fmt.Println("Given vertex is: ", v)    fmt.Println("Absolute value of given vertex is: ", ... Read More

Traverse Input Array with Boolean Flag using Arrays and Struct in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:25:52

237 Views

ExampleApproachAsk the user to enter the size of array.Make a string array of given size.Ask the user to enter elements.At the end, print the array.Example Live Demopackage main import "fmt" func main(){    arr := []int{10, 20, 30, 60, 40, 50}    boolArr := []bool{true, false, true, false, true, false}    fmt.Println("Input Array is: ", arr)    fmt.Println("Input Boolean Array is: ", boolArr)    visitedArray := []struct{       i int       b bool    }{       {10, true},       {20, false},       {30, true},       {60, false},     ... Read More

Create String Array in Go that Takes User Inputs

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:23:25

977 Views

ExampleApproachAsk the user to enter the size of array.Make a string array of given size.Ask the user to enter the elements.At the end, print the array.Example Live Demopackage main import (    "fmt" ) func main(){    fmt.Printf("Enter size of your array: ")    var size int    fmt.Scanln(&size)    var arr = make([]string, size)    for i:=0; i

Create Integer Array in Go from User Inputs

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:21:49

4K+ Views

ExampleApproachAsk the user to enter the size of array.Make an integer array of given size.Ask the user to enter elements.At the end, print the array.Example Live Demopackage main import (    "fmt" ) func main(){    fmt.Printf("Enter size of your array: ")    var size int    fmt.Scanln(&size)    var arr = make([]int, size)    for i:=0; i

Count Nodes in a Doubly Linked List in Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:19:51

155 Views

ExamplesApproachStep 1 − Define a method that accepts the head of a doubly linked list.Step 2 − Initialize temp:=head, count:=0Step 3 − Iterate temp until it becomes nil.Step 4 − Increase count by 1.Step 5 − At the end, print count.Example Live Demopackage main import "fmt" type Node struct {    prev *Node    value int    next *Node } func CreateNewNode(value int) *Node{    var node Node    node.next = nil    node.value = value    node.prev = nil    return &node } func TraverseDoublyLL(head * Node){    // Forward Traversal    fmt.Printf("Doubly Linked List: ")    count := 0 ... Read More

Create a Doubly Linked List and Traverse Forward in Golang

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:17:47

489 Views

A doubly linked list node contains three items, where two items point to the next and previous nodes, and the third item contains the value of that node.ExampleApproachStep 1 − Define a method that accepts the head of a doubly linked list.Step 2 − Initialize temp:=head.Step 3 − Iterate temp until it becomes nil.Step 4 − Print temp.value.Example Live Demopackage main import "fmt" type Node struct {    prev *Node    value int    next *Node } func CreateNewNode(value int) *Node{    var node Node    node.next = nil    node.value = value    node.prev = nil    return &node } ... Read More

Insert Node at Last Position in Linked List using Go

Rishikesh Kumar Rishi
Updated on 18-Mar-2021 12:16:15

171 Views

ExamplesExampleApproach to solve this problem Live Demopackage main import "fmt" type Node struct {    value int    next *Node } func NewNode(value int, next *Node) *Node{    var n Node    n.value = value    n.next = next    return &n } func TraverseLinkedList(head *Node){    temp := head    for temp != nil {       fmt.Printf("%d ", temp.value)       temp = temp.next    }    fmt.Println() } func InsertNodeAtIthIndex(head *Node, index, data int) *Node{    if head == nil{       head = NewNode(data, nil)       return head    }    if ... Read More

Advertisements