1. Design a shopping app using any design pattern. Write classes , explain the design.
2..Memory Management
Consider the code below:
class Student {
let name: String
init(name: String) { self.name = name }
var university: University?
deinit { print("\(name) is being deinitialized") }
}
class University {
let uniName: String
init(uniName: String) { self.uniName = uniName }
var student: Student?
deinit { print("University \(unit) is being deinitialized") }
}
Each of the classes has a reference to the other. Let’s initialise them.
var me: Student?
var uni: University?
me = Student(name: "John Doe")
uni = University(uniName: "IIT")
me?.university = uni
uni?.student = me
This creates a strong reference cycle between both the classes. How will you resolve the strong
reference cycle between both the classes?
3. In this challenge, read a text file and capture a timestamp from each line of text. Then create
a text file with a list of all timestamps that occur multiple times, each on its own line.
Naming convention and a data description are as follows:
Naming convention: You will be provided with an input file name called filename. Your
output filename should be req_filename (replace filename).
Data description: Each line of the .txt file contains a single log record for July 1995 with the
following columns in order:
1. The hostname of the host making the request.
2. This column's values are missing and described by a hyphen (i.e., -).
3. This column's values are missing and described by a hyphen (i.e., -).
4. A timestamp enclosed in square brackets following the format
[DD/mmm/YYYY:HH:MM:SS -0400], where DD is the day of the month, mmm is the
name of the month, YYYY is the year, HH:MM:SS is the time in 24-hour format, and
-0400 is the time zone.
5. The request, enclosed in quotes (e.g., "GET /images/NASA-logosmall.gif
HTTP/1.0").
6. The HTTP response code.
7. The total number of bytes sent in the response.
For example, given the following log record:
burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/video/livevideo.gif HTTP/1.0" 200 0
We can label each column in the record like so:
Hostname - - Timestamp Request HTTP Byte
Response s
Code
burger.letters.co - - [01/Jul/1995:00:00:12 "GET 200 0
m -0400] /shuttle/countdown/video/livevideo.
gif HTTP/1.0"
Given a string, filename, that denotes the name of a real text file, create an output file
named req_f ilename to store timestamp records. Each line of the output file must contain a
timestamp in the format DD/mmm/YYYY:HH:MM:SS for any timestamp that appears in more
than one request in filename. The line order in the output file does not matter.
Constraints
● The log file contains no more than 2 × 105 records.
Input Format for Custom Testing Sample Case 0
Sample Input
hosts_access_log_00.txt
Sample Output
Given filename = "hosts_access_log_00.txt", process the records in hosts_access_log_00.txt
and create an output file named req_hosts_access_log_00.txt containing the following rows:
01/Jul/1995:00:00:12
01/Jul/1995:00:00:14
01/Jul/1995:00:00:15
Explanation 0
The log file hosts_access_log_00.txt contains the following log records:
unicomp6.unicomp.net - - [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/
HTTP/1.0" 200 3985
burger.letters.com - - [01/Jul/1995:00:00:11 -0400] "GET /shuttle/countdown/liftoff.html
HTTP/1.0" 304 0
burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET /images/NASA-logosmall.gif
HTTP/1.0" 304 0
burger.letters.com - - [01/Jul/1995:00:00:12 -0400] "GET
/shuttle/countdown/video/livevideo.gif HTTP/1.0" 200 0
d104.aa.net - - [01/Jul/1995:00:00:13 -0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985
unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /shuttle/countdown/count.gif
HTTP/1.0" 200 40310
unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/NASA-logosmall.gif
HTTP/1.0" 200 786
unicomp6.unicomp.net - - [01/Jul/1995:00:00:14 -0400] "GET /images/KSC-logosmall.gif
HTTP/1.0" 200 1204
d104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /shuttle/countdown/count.gif HTTP/1.0"
200 40310
d104.aa.net - - [01/Jul/1995:00:00:15 -0400] "GET /images/NASA-logosmall.gif HTTP/1.0"
200 786
The data confirms the following:
1. The timestamp 01/Jul/1995:00:00:12 occurs two times.
2. The timestamp 01/Jul/1995:00:00:14 occurs three times.
3. The timestamp 01/Jul/1995:00:00:15 occurs two times.
Strip the brackets and time zones from the three timestamps occurring more than once and
append them to the output file.
4 .Contiguous subarrays are a group of an uninterrupted range of elements from an array as they
occur. No elements in the range can be skipped or reordered. Given an array of integers,
numbers and an integer k, determine the total number of subarrays of numbers having a
product that is less than or equal to k.
For example, contiguous subarrays of numbers = [3,4,5] are [[3], [4], [5], [3,4], [4,5], [3,4,5]].
The product of a subarray is the product of all of its elements so the products for the list of
subarrays are [3, 4, 5, 12, 20, 60]. If k = 5, there are 3 subarrays that satisfy the condition, [3],
[4] and [5].
Function Description
Complete the function count in the editor below. It must return a long integer, the number of
subarrays whose product is less than or equal to k.
count has the following parameter(s):
numbers[numbers[0],...numbers[n-1]]: an array of integers
k: an integer
Constraints
1 ≤ n ≤ 5 × 105
1 ≤ numbers[i] ≤ 102
1 ≤ k ≤ 106
Input Format for Custom Testing
Sample Case 0
Sample Input 0
3
1
2
3
4
Sample Output 0
Explanation 0
numbers = [1, 2, 3]. We have the following 6 subarrays:
[1] → 1.
[2] → 2.
[3] → 3.
[1, 2] → 1 × 2 = 2.
[2, 3] → 2 × 3 = 6.
[1, 2, 3] → 1 × 2 × 3 = 6.
The only subarrays having products less than or equal to k = 4 are [1], [2], [3], and [1, 2].
Sample Case 1
Sample Input 1
3
1
2
3
7
Sample Output 1
Explanation 1
numbers = [1, 2, 3]. We have the following 6 subarrays:
[1] → 1.
[2] → 2.
[3] → 3.
[1, 2] → 1 × 2 = 2.
[2, 3] → 2 × 3 = 6.
[1, 2, 3] → 1 × 2 × 3 = 6.
All six of the above subarrays have products less than or equal to k = 7.
1.Have the function FoodDistribution(arr) read the array of numbers stored in arr which will
represent the hunger level of different people ranging from 0 to 5 (0 meaning not hungry at all, 5
meaning very hungry). You will also have N sandwiches to give out which will range from 1 to
20. The format of the array will be [N, h1, h2, h3, ...] where N represents the number of
sandwiches you have and the rest of the array will represent the hunger levels of different
people. Your goal is to minimize the hunger difference between each pair of people in the array
using the sandwiches you have available.
// For example: if arr is [5, 3, 1, 2, 1], this means you have 5 sandwiches to give out. You can
distribute them in the following order to the people: 2, 0, 1, 0. Giving these sandwiches to the
people their hunger levels now become: [1, 1, 1, 1]. The difference between each pair of people is
now 0, the total is also 0, so your program should return 0. Note: You may not have to give out
all, or even any, of your sandwiches to produce a minimized difference.
// Another example: if arr is [4, 5, 2, 3, 1, 0] then you can distribute the sandwiches in the
following order: [3, 0, 1, 0, 0] which makes all the hunger levels the following: [2, 2, 2, 1, 0]. The
differences between each pair of people is now: 0, 0, 1, 1 and so your program should return the
final minimized difference of 2.
2.have the function PermutationStep(num) take the num parameter being passed and return the
next number greater than num using the same digits.
//For example: if num is 123 return 132, if it's 12453 return 12534.
//If a number has no greater permutations, return -1 (ie. 999).
// Sample Test Cases
// Input:11121
// Output:11211
// Input:41352
// Output:41523
3.Have the function OverlappingRanges(arr) take the array of numbers stored in arr which will
contain 5 positive integers, the first two representing a range of numbers (a to b), the next 2 also
representing another range of integers (c to d), and a final 5th element (x) which will also be a
positive integer, and return the string true if both sets of ranges overlap by at least x numbers.
For example: if arr is [4, 10, 2, 6, 3] then your program should return the string true. The first
range of numbers are 4, 5, 6, 7, 8, 9, 10 and the second range of numbers are 2, 3, 4, 5, 6. The
last element in the array is 3, and there are 3 numbers that overlap between both ranges: 4, 5,
and 6. If both ranges do not overlap by at least x numbers, then your program should return the
string false.