Last active
April 2, 2020 08:54
-
-
Save williamhqs/c04f2e7c9fff43576aafc8561ac6bcc9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 3.1 */ | |
// 1. Write Pseudo code find a given element in an array | |
func linearSearch<T: Equatable>(target: T, in array: [T]) { | |
for (index, element) in array.enumerated() { | |
if element == target { | |
print("Found \(element) at \(index)") | |
break | |
} else if (index == array.count) { | |
print("The target \(target) not found") | |
} | |
} | |
} | |
let targetNumber = 17 | |
let array = [2, 3, 8, 1, 7, 9, 2] | |
linearSearch(target: 7, in: array) | |
let targetString = "G" | |
let stringArray = ["B", "C", "G", "D"] | |
linearSearch(target: targetString, in: stringArray) | |
// 2. Write Pseudo code add an element at a given position in an array | |
var array1 = [1, 2, 4, 9, 3] | |
func add<T>(element: T, into array: inout[T], at index: Int, placeHolder: T) { | |
let newSize = array.count + 1 | |
var newArray = [T](repeating: placeHolder, count: newSize) | |
for (index, value) in array.enumerated() { | |
newArray[index] = value | |
} | |
for position in stride(from: newSize - 1, to: index, by: -1) { | |
newArray[position] = newArray[position - 1] | |
} | |
newArray[index] = element | |
array = newArray | |
} | |
add(element: 100, into: &array1, at: 3, placeHolder: 0) | |
print(array1) | |
//3. Write Pseudo code remove an element at a given position in an array | |
var array2 = [1, 2, 4, 9, 3] | |
func removeElement<T>(at index: Int, from array: inout [T], placeHolder: T) { | |
var targetArray = [T](repeating: placeHolder, count: array.count - 1) | |
var startRemove = false | |
for (position, element) in array.enumerated() { | |
if position == index { | |
startRemove = true | |
} else { | |
if !startRemove { | |
targetArray[position] = element | |
} else if startRemove { | |
targetArray[position-1] = element | |
} | |
} | |
} | |
array = targetArray | |
} | |
removeElement(at: 2, from: &array2, placeHolder: -1) | |
print(array2) | |
// 4. Write Pseudo code to merge the given two arrays | |
var array3 = [1, 2, 4, 9, 3] | |
var array4 = [8, 4, 9, 3] | |
func merge<T>(array1: [T], array2: [T], placeHolder: T) -> [T] { | |
var targetArray = [T](repeating: placeHolder, count: array1.count + array2.count) | |
for (position, element) in array1.enumerated() { | |
targetArray[position] = element | |
} | |
for (position, element) in array2.enumerated() { | |
targetArray[position + array1.count] = element | |
} | |
return targetArray | |
} | |
let result = merge(array1: array3, array2: array4, placeHolder: -1) | |
print(result) | |
3.2 LinkedList | |
//Task02: Linked List | |
//1. Write Pseudo code find a given element in a linked list | |
//2. Write Pseudo code add an element at a given position in a linked list | |
//3. Write Pseudo code remove an element at a given position in a linked list | |
//4. Write Pseudo code to merge the given two linked lists | |
class Node<T: Comparable> { | |
var value: T | |
var next: Node? | |
init(value: T) { | |
self.value = value | |
} | |
} | |
class LinkedList { | |
var first: Node<Int>? | |
var last: Node<Int>? { | |
guard var node = first else { | |
return .none | |
} | |
while let next = node.next { | |
node = next | |
} | |
return node | |
} | |
func append(value: Int) { | |
guard let _ = first else { | |
self.first = Node(value: value) | |
return | |
} | |
last?.next = Node(value: value) | |
} | |
func find(value: Int) { | |
guard var node = first else { | |
return | |
} | |
while let next = node.next { | |
if next.value == value { | |
print("Found") | |
} | |
node = next | |
} | |
} | |
func printMe() { | |
var listString = "" | |
guard var node = first else { | |
return | |
} | |
listString = String(node.value) | |
while let next = node.next { | |
listString += "->" + String(next.value) | |
node = next | |
} | |
print(listString) | |
} | |
func append(value: Int, at position: Int) { | |
if position == 0 { | |
print("Sorry, the min position is 1") | |
return | |
} | |
guard var node = first else { | |
return | |
} | |
if position == 1 { | |
let newNode = Node(value: value) | |
newNode.next = node.next | |
node.next = newNode | |
} else { | |
for index in 1..<position { | |
if let next = node.next { | |
if index == position - 1 { | |
let newNode = Node(value: value) | |
newNode.next = next.next | |
next.next = newNode | |
} else { | |
node = next | |
} | |
} | |
} | |
} | |
} | |
func remove(at position: Int) { | |
if position == 0 { | |
print("Sorry, the min position is 1") | |
return | |
} | |
guard var node = first else { | |
return | |
} | |
if position == 1 { | |
first = node.next | |
} else { | |
for index in 1..<position { | |
if let next = node.next { | |
if index == position-1 { | |
node.next = node.next?.next | |
} | |
node = next | |
} | |
} | |
} | |
} | |
func merge(list: LinkedList) { | |
last?.next = list.first | |
} | |
} | |
//// //1. Write Pseudo code find a given element in a linked list | |
print("1. Write Pseudo code find a given element in a linked list") | |
let linkedList = LinkedList() | |
linkedList.append(value: 5) | |
linkedList.append(value: 6) | |
linkedList.append(value: 8) | |
linkedList.append(value: 7) | |
linkedList.printMe() | |
linkedList.find(value: 18) | |
linkedList.find(value: 8) | |
print("2. Write Pseudo code add an element at a given position in a linked list") | |
linkedList.printMe() | |
linkedList.append(value: 100, at: 3) | |
linkedList.printMe() | |
print("3. Write Pseudo code remove an element at a given position in a linked list") | |
print("Before remove:") | |
linkedList.printMe() | |
linkedList.remove(at: 3) | |
print("After remove:") | |
linkedList.printMe() | |
print("4. Write Pseudo code to merge the given two linked lists") | |
let linkedList1 = LinkedList() | |
linkedList1.append(value: 50) | |
linkedList1.append(value: 60) | |
linkedList.merge(list: linkedList1) | |
linkedList.printMe() | |
//The home taks has below 2 tasks. You need to complete both the tasks. Maximum marks : 12 marks | |
// | |
//================================================= | |
//Task 01: Stack | |
//1. Write Pseudo code to implement stack using Arrays | |
//2. Write Pseudo code to implement stack using Linked list. | |
// | |
//Marks: | |
//Successful creation of Pseudocode -> 4 Marks (2 for each) | |
//Divide logic into modules -> 1 extra mark | |
//Optimized logic -> 1 extra mark (Please explain the optimization you have done to your mentor) | |
// | |
//Maximum marks -> 6 marks | |
// | |
//===================================================== | |
//Task02: Queue | |
//1. Write Pseudo code to implement queue using Arrays | |
//2. Write Pseudo code to implement queue using Linked list. | |
// | |
//Marks: | |
//Successful creation of Pseudocode -> 4 Marks (2 for each) | |
//Divide logic into modules -> 1 extra mark | |
//Optimized logic -> 1 extra mark (Please explain the optimization you have done to your mentor) | |
// | |
//Maximum marks -> 6 marks | |
//Task 01: Stack | |
//1. Write Pseudo code to implement stack using Arrays | |
//2. Write Pseudo code to implement stack using Linked list. | |
// | |
class Stack { | |
var values = [Int]() | |
func push(element: Int) { | |
values.append(element) | |
} | |
func pop() { | |
values.removeLast() | |
} | |
func debugPrint() { | |
let result = values.map { | |
"|" + String($0) + "|" + "\n" | |
}.reduce("", +) | |
print(result) | |
} | |
} | |
var stack = Stack() | |
stack.push(element: 2) | |
stack.push(element: 3) | |
stack.debugPrint() | |
stack.pop() | |
stack.debugPrint() | |
class Node<T: Comparable> { | |
var value: T | |
var next: Node? | |
init(value: T) { | |
self.value = value | |
} | |
} | |
class LinkedList { | |
var first: Node<Int>? | |
var last: Node<Int>? { | |
guard var node = first else { | |
return .none | |
} | |
while let next = node.next { | |
node = next | |
} | |
return node | |
} | |
var count: Int { | |
guard var node = first else { | |
return 0 | |
} | |
var index = 1 | |
while let next = node.next { | |
index += 1 | |
node = next | |
} | |
return index | |
} | |
func append(value: Int) { | |
guard let _ = first else { | |
self.first = Node(value: value) | |
return | |
} | |
last?.next = Node(value: value) | |
} | |
func find(value: Int) { | |
guard var node = first else { | |
return | |
} | |
while let next = node.next { | |
if next.value == value { | |
print("Found") | |
} | |
node = next | |
} | |
} | |
func printMe() { | |
var listString = "" | |
guard var node = first else { | |
return | |
} | |
listString = String(node.value) | |
while let next = node.next { | |
listString += "->" + String(next.value) | |
node = next | |
} | |
print(listString) | |
} | |
func append(value: Int, at position: Int) { | |
if position == 0 { | |
print("Sorry, the min position is 1") | |
return | |
} | |
guard var node = first else { | |
return | |
} | |
if position == 1 { | |
let newNode = Node(value: value) | |
newNode.next = node.next | |
node.next = newNode | |
} else { | |
for index in 1..<position { | |
if let next = node.next { | |
if index == position - 1 { | |
let newNode = Node(value: value) | |
newNode.next = next.next | |
next.next = newNode | |
} else { | |
node = next | |
} | |
} | |
} | |
} | |
} | |
func remove(at position: Int) { | |
if position == 0 { | |
print("Sorry, the min position is 1") | |
return | |
} | |
guard var node = first else { | |
return | |
} | |
if position == 1 { | |
first = node.next | |
} else { | |
for index in 1..<position { | |
if let next = node.next { | |
if index == position-1 { | |
node.next = node.next?.next | |
} | |
node = next | |
} | |
} | |
} | |
} | |
func merge(list: LinkedList) { | |
last?.next = list.first | |
} | |
} | |
class Stack1 { | |
var values = LinkedList() | |
func push(element: Int) { | |
values.append(value: element) | |
} | |
func pop() { | |
values.remove(at: values.count) | |
} | |
} | |
let stack1 = Stack1() | |
stack1.push(element: 5) | |
stack1.push(element: 6) | |
stack1.values.printMe() | |
stack1.pop() | |
stack1.values.printMe() | |
class Queue { | |
var values = [Int]() | |
func push(element: Int) { | |
values.append(element) | |
} | |
func pop() -> Int{ | |
return values.remove(at: 0) | |
} | |
func printMe() { | |
let result = values.map { | |
"|" + String($0) + "|" + "\n" | |
}.reduce("", +) | |
print(result) | |
} | |
} | |
var queue = Queue() | |
queue.push(element: 1) | |
queue.push(element: 2) | |
queue.push(element: 5) | |
queue.push(element: 8) | |
queue.printMe() | |
queue.pop() | |
queue.printMe() | |
queue.pop() | |
queue.printMe() | |
class Queue1 { | |
var values = LinkedList() | |
func push(element: Int) { | |
values.append(value: element) | |
} | |
func pop() { | |
values.remove(at: 0) | |
} | |
func printMe() { | |
values.printMe() | |
} | |
} | |
var queue1 = Queue() | |
queue1.push(element: 1) | |
queue1.push(element: 2) | |
queue1.push(element: 5) | |
queue1.push(element: 8) | |
queue1.printMe() | |
queue1.pop() | |
queue1.printMe() | |
queue1.pop() | |
queue1.printMe() | |
//Maximum marks : 7 marks | |
// | |
//================================================= | |
//Task 01: Hash Table | |
//Write Pseudo code to find out a frequency of a given number in an array. | |
// | |
// | |
//Marks: | |
//Successful creation of Pseudocode -> 5 Marks | |
//Divide logic into modules -> 1 extra mark | |
//Optimized logic -> 1 extra mark (Please explain the optimization you have done to your mentor) | |
// | |
//Maximum marks -> 7 marks | |
var array = [1, | |
2, 2, 2, | |
8, 8, | |
9, | |
10, | |
12] | |
func findFrequency(of array: [Int]) -> [String: Int] { | |
var result = [String: Int]() | |
array.forEach { element in | |
let key = String(element) | |
if var count = result[key] { | |
count += 1 | |
result[key] = count | |
} else { | |
result[key] = 1 | |
} | |
} | |
return result | |
} | |
print(findFrequency(of: array)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment