0% found this document useful (0 votes)
3 views6 pages

CS201 Midterm2 Helper Sheet

The document provides an overview of various data structures and algorithms, including ArrayLists, HashSets, HashMaps, and linked lists, detailing their key methods and performance characteristics. It also covers the AbstractMarkovModel and DNA manipulation interfaces, highlighting their functionalities and runtime complexities. Additionally, it discusses different implementations for DNA strands, comparing their efficiency and use cases.

Uploaded by

6km456927g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

CS201 Midterm2 Helper Sheet

The document provides an overview of various data structures and algorithms, including ArrayLists, HashSets, HashMaps, and linked lists, detailing their key methods and performance characteristics. It also covers the AbstractMarkovModel and DNA manipulation interfaces, highlighting their functionalities and runtime complexities. Additionally, it discusses different implementations for DNA strands, comparing their efficiency and use cases.

Uploaded by

6km456927g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

List.

contains(“hello”)
ArrayList List.get(i)
ArrayList<String> students = new ArrayList<String>();
ArrayList can’t hold primitive like int,
char
studentList.add("John"); BUT use Integer, Character
studentList.remove(0);
studentList.remove("Lily"); Traverse array:
int bat = Collections.frequency(list,”bat”) for (int i = 0; i <
arrayName.length; i++)
Array
for (dataType element :
dataType[] arrayName = new dataType[size];
Changing Elements: arrayName[index] = value arrayName)
arrayName.length

String[] words = orginal.split(“ “)


Growing array my multiplying is linear but by
adding elements is quadratic
Iterator<Integer> it =
Hash Set
numbers.iterator();
while (it.hasNext()) {
HashSet<dataType> setName = new HashSet<>(); System.out.println(it.next());
setName.add(element);
int size = setName.size();

Iterating through a HashSet // Example:


Using Iterator
Iterator<dataType> iterator = setName.iterator();
for (Integer num : numbers) {
while (iterator.hasNext()) { System.out.println(num);
dataType element = iterator.next(); }
System.out.println(element); Constructor with parameters
//instance variables
Using Enhanced for-loop (for-each loop) String make;
for (dataType element : setName) {
String model;
System.out.println(element);
} int year;
public Car(String make, String model, int
HashSet<Type> Set = new HashSet<>(Arrays.asList(s)) year)
- Takes array s and make it into HashSet {
this.make = make;
this.model = model;
Hash Map this.year = year;
HashMap<KeyType, ValueType> mapName = new HashMap<>();
mapName.put(key, value); Default Constructor
public Car()
ValueType value = mapName.get(key); model = “”;
boolean exists = mapName.containsKey(key); year””;
boolean exists = mapName.containsValue(value);
Map.putIfAbsent(key, value)
ValueType value = mapName.getOrDefault(key, defaultValue);
If value is arrayList and you
Iterating through Keys want to add to arraylist use:
for (KeyType key : mapName.keySet()) List.get(key).add(value)
String methods, length(), .charAt(i), .substring(a,b), s.toLowerCase()

Static belongs to class not object just like Math class

Join array of string: String.join(“ “, words)

P3 CHAT MARKOV
1. AbstractMarkovModel Class
 Purpose:
Abstract base class for Markov models, defining core methods for text-based training and
generation.
1. setTraining(String text): Abstract, splits text into words.
Runtime: O(n), where n is the number of words.
2. getFollows(WordGram wgram): Abstract, retrieves words following a WordGram.
Runtime: O(n) (BaseMarkov) / O(1) (HashMarkov).
3. getRandomNextWord(WordGram wgram): Picks a random word following a
WordGram.
Runtime: O(1).
4. getRandomGram(): Generates a random WordGram.
Runtime: O(k), where k is the WordGram length.

2. WordGram Class
 Purpose: Represents an immutable sequence of words.
 Key Methods and Runtime:
1. equals(Object o): Checks if two WordGram objects are equal.
Runtime: O(k).
2. shiftAdd(String last): Returns a new WordGram with a word shifted in.
Runtime: O(k).
3. BaseMarkovModel Class
 Purpose:
Uses sequential search to find words following a WordGram.
 Key Methods and Runtime:
1. setTraining(String text): Splits text into words.
Runtime: O(n).
2. getFollows(WordGram wgram): Searches through the entire text.
Runtime: O(n) per call.
4. HashMarkovModel Class
 Purpose:
Uses a HashMap to pre-compute and store follow-word mappings for faster lookups.
 Key Methods and Runtime:
1. setTraining(String text): Populates a HashMap with WordGram mappings.
Runtime: O(n).
2. getFollows(WordGram wgram): Looks up words from the HashMap.
Runtime: O(1) average, O(n) worst-case.

5. Key Differences Between BaseMarkovModel and HashMarkovModel


1. Data Structure:
o BaseMarkovModel: Uses arrays and sequential search.
o HashMarkovModel: Uses a HashMap for pre-computed mappings.
2. Training Process:
o BaseMarkovModel: No pre-computation; searches text each time.
Runtime: O(n) per getFollows() call.
o HashMarkovModel: Pre-computes mappings in HashMap.
Runtime: O(n) during training.
3. getFollows() Performance:
o BaseMarkovModel: O(n) for each call (sequential search).
o HashMarkovModel: O(1) average (HashMap lookup).
4. Text Generation Performance:
o BaseMarkovModel: Slower.
Overall Time: O(m * n), where m is the number of words generated.
o HashMarkovModel: Faster.
Overall Time: O(m), assuming O(1) lookups.

6. How the Code Works


1. Training Phase:
o Splits the text into words.
o Generates WordGrams of a specified order.
2. Generating Random Text:
o Picks a random starting WordGram.
o Uses the model to find possible next words.
o Selects a random word and repeats until desired length is reached.

P4 DNA
The IDnaStrand interface specifies the operations required to manipulate DNA strands.
 Key Methods:
1. cutAndSplice: Replaces occurrences of a given enzyme sequence with another splicee
sequence.
Search for Enzyme:
 iterate through the strand, searching for the enzyme using indexOf().
 Every time it finds an occurrence of the enzyme:
o It extracts the segment before the enzyme.
o Replaces the enzyme with the splicee string.
Build the New Strand:
 If this is the first match, a new strand is created using getInstance().
 For subsequent matches, the new segments are appended to the existing strand.
 After each replacement, the start index is moved to the end of the current enzyme
match.

2. size: Returns the length of the strand.
3. append: Adds DNA sequences to the end of the strand.
4. reverse: Reverses the DNA strand.
5. charAt: Accesses a character at a given index.
6. getInstance: Creates a new instance of the DNA strand type.
Shift Add: Shifting the existing sequence to the left (removing the first word). Adding a new word
to the end of the sequence.

StringStrand
 Description: Stores the DNA data as a simple String.
 Key Operations:
o Appending is performed by creating a new string each time, which leads to high overhead.
o reverse() creates a new reversed string using StringBuilder.
 Runtime Analysis:
1. append(): O(n) (new string created each time).
2. reverse(): O(n) (creating and reversing with StringBuilder).
3. cutAndSplice(): O(m*n) (repeated string operations during search/replace).
 Pros:
o Simple to implement and straightforward.
 Cons:
o Slow for large inputs due to string immutability and frequent copying.

StringBuilderStrand
 Description: Uses a StringBuilder for better performance with dynamic strings.
 Key Operations:
o Appending is performed directly on the StringBuilder, avoiding repeated copying.
o reverse() reverses the content in-place with StringBuilder.
 Runtime Analysis:
1. append(): O(1) amortized (no new string creation).
2. reverse(): O(n) (in-place reversal).
3. cutAndSplice: O(m*n) (still linear search but better append performance).
 Pros:
o Faster appends compared to StringStrand.
 Cons:
o Still not optimal for very large data due to the sequential nature of string operations.

LinkStrand
 Description: Uses a linked list (Node) to store DNA segments. Each append adds a new node to
the list.
 Key Operations:
o Appending is constant time by adding new nodes.
o reverse() reverses the linked list by creating new nodes in reverse order.
 Runtime Analysis:
1. append(): O(1) (constant time with linked list).
2. reverse(): O(n) (traversing and creating new nodes).
3. cutAndSplice(): O(m + n) (efficient due to constant-time appends).
 Pros:
o Best for large data due to constant-time appends.
 Cons:
o More complex implementation compared to string-based approaches.

Linked List public class ListStretch {


public class ListsEqual { public ListNode stretch (ListNode list, int
public int equal (ListNode a1, ListNode a2) { amount){
while(a1 != null && a2!=null){ if(list == null){
if(a1.info != a2.info){ return null; }
return 0; } ListNode head = null;
a1 = a1.next; ListNode current = null;
a2 = a2.next; while(list != null){
} for (int i=0; i<amount; i++){
if(a1==null && a2==null){ ListNode newNode = new
return 1; ListNode(list.info);
} if(head == null){
else{ head = newNode;
return 0; } }} current = newNode; }
else{
current.next= newNode;
current = current.next; } }
public class ListSum { list=list.next;}
public int sum(ListNode list, int thresh) { return head;
int count = 0; }}
public class ListLastFirst {
while (list !=null){ public ListNode move(ListNode list) {
if(list.info > thresh){ if (list == null){
count = count + list.info; return null;
} }
list = list.next; if (list.next == null){
} return list;
return count; }
}}
ListNode current = list;
while (current.next.next != null){
current=current.next; }
ListNode lastNode = current.next;
current.next = null;
lastNode.next = list;
return lastNode;
RECURSION }}Copy List

You might also like