MySQL Angular 12 Django CRUD Example

Tutorial: Django Angular 12 CRUD Example – Fullstack: Angular 12 + Django Rest Framework + MySQL.

Django is a Python-based free and open-source web framework that follows the model-template-view architectural pattern. Angular is a TypeScript-based open-source web application framework led by the Angular Team at Google. In the tutorial, I introduce how to build a Angular Django CRUD RestAPIs Fullstack Project with POST/GET/PUT/DELETE requests using Django Rest Framework and MySQL database with step by step coding examples.

Related posts:


Continue reading “MySQL Angular 12 Django CRUD Example”

Django Angular 12 PostgreSQL CRUD Example

Django is a Python-based free and open-source web framework that follows the model-template-view architectural pattern. Angular 12 is a TypeScript-based open-source web application framework led by the Angular Team at Google. In the tutorial, I introduce how to build a “Django Python Angular 12 PostgreSQL CRUD RestAPI Example” with Fullstack Projects providing POST/GET/PUT/DELETE requests using Django Rest Framework and PostgreSQL database with step by step coding examples:

– I draw a fullstack overview Diagram Architecture from Angular 12 Frontend to PostgreSQL database through Django RestAPI backend.
– Develop Django CRUD RestAPIs with the supporting of Django Rest Framework.
– Implement Angular 12 CRUD application with the Angular 12 Httpclient to do CRUD request (Post/Get/Put/Delete) to Django Backend APIs.
– I create a testsuite with a number of integrative testcases with CRUD RestAPI requests from Angular 12 HttpClient to do CRUD requests to Django RestAPIs Server and save/retrieve data to PostgreSQL database.

Related posts:


Continue reading “Django Angular 12 PostgreSQL CRUD Example”

Kotlin Solution with Codility Example – Find The Smallest Positive Integer

Write a function:
fun solution(A: IntArray): Int
that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.
For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.
Given A = [1, 2, 3], the function should return 4.
Given A = [−1, −3], the function should return 1.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].

Solution using HashMap

– BigO = O(n)
Kotlin Coding:


import java.util.*

fun main() {
    var original: IntArray = intArrayOf(1, 2, 3)
    solution(original)
}

fun solution(A: IntArray): Int {
	if (A.size == 0)
		return 1
	
    var max = A[0]

    for (num in A) {
        if (max < num)
            max = num
    }


    var min = A[0]

    for (num in A) {
        if (min > num)
            min = num
    }

	if(max < 1) return 1
	if(min > 1) return 1
	
	var hashMap : HashMap<Int, Int> = HashMap<Int, Int> ()
	
	for ((index, value) in A.withIndex()) {
		hashMap.put(value , index)
	}
	
    val len = A.size - 1
    
	for (i in 1..len) {
		if(hashMap.get(i) == null){
			return i
        }
	}
	
	return A[A.size - 1] + 1
}

Solution 2 using Sorting

– BigO = O(nlg(n))

Kotlin Coding:


import java.util.*

fun main() {
    var original: IntArray = intArrayOf(4, 5, 3, -1, 11, -2, 1)
    println(solution(original))
}

fun solution(A: IntArray): Int {
	A.sort()	
	
	var indexPositiveStart = -1
	
	for((index, value) in A.withIndex()) {
		if (value > 0) {
			indexPositiveStart = index
			break
		}
	}
	if (indexPositiveStart == -1)
		return 1
	
	if(A[indexPositiveStart] > 1){
		return 1
	}
	
	for (i in indexPositiveStart until (A.size - 1)){
		// check next item
		if (i + 1 <= A.size - 1) {
			if (A[i] + 1 < A[i+1])
				return A[i] + 1
		}
	}
	
	return A[A.size - 1] + 1
}

Kotlin Count number of occurrences (or frequency) in a sorted array

Count number of occurrences (or frequency) in a sorted array
Expected time complexity is O(Logn)

– Example:


Input: arr[] = {1, 1, 2, 2, 2, 2, 3,},   x = 2
Output: 4 // x (or 2) occurs 4 times in arr[]

Solution -> Improved Binary Search
1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);


import java.util.*

fun main() {
	var A = intArrayOf(1, 2, 2, 3, 4)
	println(solution(A, 2))
}

fun solution(A: IntArray, x: Int): Int {
	
	if (A.size == 0)
		return -1
	
	var leftOccur = binarySearchLeftMostOccurs(A, x, 0, A.size - 1)
	var rightOccur = leftOccur
	
	if (leftOccur == -1)
		return -1
	else {
		rightOccur = binarySearchRightMostOccurs(A, x, leftOccur, A.size - 1)
	} 
	
	return rightOccur - leftOccur + 1
}

fun binarySearchLeftMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {

	if(leftmost == rightmost) {
		if (A[leftmost] == x) {
			return leftmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == leftmost) || (A[pivot - 1] < A[pivot]))) {
		return pivot
	}
	
	if(A[pivot] >= x){
		return binarySearchLeftMostOccurs(A, x, leftmost, pivot)
	}else {
		return binarySearchLeftMostOccurs(A, x, pivot + 1, rightmost)
	}
}

fun binarySearchRightMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {
	if(leftmost == rightmost) {
		if (A[rightmost] == x) {
			return rightmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == rightmost) || (A[pivot] < A[pivot + 1]))) {
		return pivot
	}
	
	if(A[pivot] <= x){
		return binarySearchRightMostOccurs(A, x, pivot + 1, rightmost)
	}else {
		return binarySearchRightMostOccurs(A, x, leftmost, pivot)
	}
}

Kotlin Coding with Codility

Content Table:


Find Out the number of interesting Clock with Kotlin


fun main() {
	val count = solution("15:15:00", "15:15:12")
	println(count)
}

fun solution(S: String, T: String): Int {
	val s = S.split(":")
	val t = T.split(":")
	
	val sH = strToTime(s[0])
	val sM = strToTime(s[1])
	val sS = strToTime(s[2])
	
	val tH = strToTime(t[0])
	val tM = strToTime(t[1])
	val tS = strToTime(t[2])
	
	var total = 0
	
	for (h in sH..tH) {
		if(h < tH) {
			for (m in 0..59) {
				for (s in 0..59) {
					if(interestingClock(h,m,s)) total++
				}
			}
		} else {
			for (m in sM..tM) {
				if(m < tM) {
					for (s in 0..59) {
						if(interestingClock(h,m,s)) total++
					}				
				}else {
					for (s in sS..tS) {
						if(interestingClock(h,m,s)) total++
					}				
				}
			}			
		}
	}
	
	return total
}

fun interestingClock(h: Int, m: Int, s: Int): Boolean {
	val list: MutableList<Int> = arrayListOf()
    list.addAll(numToList(h))
    list.addAll(numToList(m))
	list.addAll(numToList(s))
   
    if (list.distinct().count() <= 2)
		return true
	else 
    	return false
}

fun numToList(num: Int): List<Int> {
	val list: MutableList<Int> = mutableListOf()
    if(num < 10){
    	list.add(0)
    	list.add(num)
    }else {
    	list.add(num % 10)
    	list.add(num/10)
    }
    
    return list
}

fun strToTime(s:String): Int {
	if(s.startsWith("0")){
		val chars: Array<Char> = s.toCharArray().toTypedArray()
		return Character.getNumericValue(chars[1])
	}
	
	return s.toInt()
}

Count number of occurrences (or frequency) in a sorted array

Count number of occurrences (or frequency) in a sorted array
Expected time complexity is O(Logn)

– Example:


Input: arr[] = {1, 1, 2, 2, 2, 2, 3,},   x = 2
Output: 4 // x (or 2) occurs 4 times in arr[]

Solution -> Improved Binary Search
1) Use Binary search to get index of the first occurrence of x in arr[]. Let the index of the first occurrence be i.
2) Use Binary search to get index of the last occurrence of x in arr[]. Let the index of the last occurrence be j.
3) Return (j – i + 1);


import java.util.*

fun main() {
	var A = intArrayOf(1, 2, 2, 3, 4)
	println(solution(A, 2))
}

fun solution(A: IntArray, x: Int): Int {
	
	if (A.size == 0)
		return -1
	
	var leftOccur = binarySearchLeftMostOccurs(A, x, 0, A.size - 1)
	var rightOccur = leftOccur
	
	if (leftOccur == -1)
		return -1
	else {
		rightOccur = binarySearchRightMostOccurs(A, x, leftOccur, A.size - 1)
	} 
	
	return rightOccur - leftOccur + 1
}

fun binarySearchLeftMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {

	if(leftmost == rightmost) {
		if (A[leftmost] == x) {
			return leftmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == leftmost) || (A[pivot - 1] < A[pivot]))) {
		return pivot
	}
	
	if(A[pivot] >= x){
		return binarySearchLeftMostOccurs(A, x, leftmost, pivot)
	}else {
		return binarySearchLeftMostOccurs(A, x, pivot + 1, rightmost)
	}
}

fun binarySearchRightMostOccurs(A: IntArray, x: Int, leftmost: Int, rightmost: Int): Int {
	if(leftmost == rightmost) {
		if (A[rightmost] == x) {
			return rightmost
		}
		
		return -1
	}
	
	var pivot = (leftmost + rightmost) / 2
	
	if(A[pivot] == x && ((pivot == rightmost) || (A[pivot] < A[pivot + 1]))) {
		return pivot
	}
	
	if(A[pivot] <= x){
		return binarySearchRightMostOccurs(A, x, pivot + 1, rightmost)
	}else {
		return binarySearchRightMostOccurs(A, x, leftmost, pivot)
	}
}