Aleksiej's Blog

Blog

Welcome to my blog about programming, machine learning, philosophy, and sociology.
Tag cloud  All words

Median of Two Sorted Arrays

                       
                
                
/*

#-------------------------------------------------------------#
#                                                             #
#  version 0.0.1                                              #
#  https://leetcode.com/problems/median-of-two-sorted-arrays/ #
#                                                             #
#  Aleksiej Ostrowski, 2020                                   #
#                                                             #
#  https://aleksiej.com                                       #
#                                                             #
#-------------------------------------------------------------#  

*/

package main

import (
    "fmt"
)

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {

    i1 := 0
    i2 := 0

    var x1 []int
    var x2 []int

    x1 = nums1
    x2 = nums2

    switch {
    case (len(nums1) == 0) && (len(nums2) > 0):
         x1 = nums2
         x2 = nums1
    case (len(nums1) > 0) && (len(nums2) > 0):
        if nums1[i1] > nums2[i2] {
            x1 = nums2
            x2 = nums1
        }
    }

    r := make([]int, len(x1) + len(x2))

    ir := 0

    for {

        if i1 >= len(x1) {
            break
        }

        e1 := x1[i1]

        r[ir] = e1

        ir += 1

        e3 := e1

        flag := true

        if i1 + 1 < len(x1) {
            e3 = x1[i1 + 1]
            flag = false
        }

        for {

            if i2 >= len(x2) {
                break
            }

            e2 := x2[i2]
            if (e2 >= e1) && ( (e2 <= e3) || flag) {
                r[ir] = e2
                ir += 1
                i2 += 1
            } else {
                break;
            }
        }

        i1 += 1
    }

    ifz := len(r) % 2 == 0

    fmt.Println(r)

    ind := len(r) / 2

    fmt.Println("ind = ", ind, "el = ", r[ind])
    
    if ifz {
        return (float64(r[ind - 1]) + float64(r[ind])) * 0.5
    } else {
        return float64(r[ind])
    }

}


func main() {

    a := []int{1,34,56,100,200, 201, 500}
    b := []int{250}

    // a := []int{1,2,3,4,5}
    // b := []int{}

    fmt.Println("a = ", a)
    fmt.Println("b = ", b)

    fmt.Println(findMedianSortedArrays(a, b))

}
                
                
                
                

Two Sum

					    

	
#-----------------------------------------#
#                                         #
#  version 0.0.1                          #
#  https://leetcode.com/problems/two-sum/ #
#                                         #
#  Aleksiej Ostrowski, 2020               #
#                                         #
#  https://aleksiej.com                   #
#                                         #
#-----------------------------------------#  

from itertools import combinations

class Solution(object):

    def __init__(self, nums, target):
        self.nums = nums
        self.target = target

    def twoSum(self):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """

        new_nums = sorted(list(enumerate(self.nums)), key = lambda x: x[1])

        for x in combinations(range(len(new_nums)), 2):    
            if new_nums[x[0]][1] + new_nums[x[1]][1] == self.target:    
                return list((new_nums[x[0]][0], new_nums[x[1]][0]))

'''
nums = [0,4,3,0]
target = 0

nums = [2, 20, 100, 7, 11, 15] 
target = 9
'''

nums = [-3,4,3,90]
target = 0

print(nums)
print(target)

sol = Solution(nums, target)
print(sol.twoSum())
	


Add Two Numbers

                       
                
                
/*

#---------------------------------------------------#
#                                                   #
#  version 0.0.1                                    #
#  https://leetcode.com/problems/add-two-numbers/   #
#                                                   #
#  Aleksiej Ostrowski, 2020                         #
#                                                   #
#  https://aleksiej.com                             #
#                                                   #
#---------------------------------------------------#

*/

package main

import (
    "fmt"
)

type ListNode struct {
    Val  int
    Next *ListNode
}

/*

func reverseList(l *ListNode) *ListNode {

    temp := []int{}

    x := l

    for {

        if x == nil {
            break
        }

        temp = append(temp, x.Val)

        x = x.Next
    }

    // https://stackoverflow.com/questions/19239449/how-do-i-reverse-an-array-in-go
    for i, j := 0, len(temp)-1; i < j; i, j = i+1, j-1 {
        temp[i], temp[j] = temp[j], temp[i]
    }

    var newl = &ListNode{0, nil}

    var start = newl

    for i, e := range temp {
        newl.Val = e
        if i != len(temp)-1 {
            newl.Next = &ListNode{0, nil}
            newl = newl.Next
        }
    }

    return start
}
*/

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {

    if (l1 == nil) || (l2 == nil) {
        return nil
    }

    x1 := l1
    x2 := l2

    var newL = &ListNode{0, nil}

    var start = newL

    flag1 := true
    flag2 := true

    extra := 0

    for {

        newVal := extra

        if flag1 {
            newVal += x1.Val
        }

        if flag2 {
            newVal += x2.Val
        }

        extra = 0

        if newVal > 9 {
            extra = 1
            newVal -= 10
        }

        newL.Val = newVal

        if flag1 {
            x1 = x1.Next
            flag1 = x1 != nil
        }

        if flag2 {
            x2 = x2.Next
            flag2 = x2 != nil
        }

        if flag1 || flag2 || (extra > 0) {
            newL.Next = &ListNode{0, nil}
            newL = newL.Next
        } else {
            break
        }
    }

    return start

}

func ListCreate(a []int) *ListNode {

    var l = &ListNode{0, nil}

    var start = l

    for i, s := range a {

        l.Val = s
        if i != len(a)-1 {
            l.Next = &ListNode{0, nil}
            l = l.Next
        }
    }

    return start
}

func printList(l *ListNode) {

    x := l

    for {

        if x == nil {
            break
        }

        fmt.Print(x.Val, " ")

        x = x.Next
    }

    fmt.Println()

}

func main() {

    a := []int{2, 4, 3}
    b := []int{5, 6, 4}

    l1 := ListCreate(a)
    l2 := ListCreate(b)

    printList(l1)
    printList(l2)

    printList(addTwoNumbers(l1, l2))

    // 7 0 8
}