weiweiz1 commited on
Commit
e0cfa41
·
verified ·
1 Parent(s): c7bee64

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +115 -14
README.md CHANGED
@@ -37,7 +37,7 @@ model = AutoModelForCausalLM.from_pretrained(
37
  )
38
 
39
  # prepare the model input
40
- prompt = "Give me a short introduction to large language model."
41
  messages = [
42
  {"role": "user", "content": prompt}
43
  ]
@@ -53,22 +53,123 @@ generated_ids = model.generate(
53
  **model_inputs,
54
  max_new_tokens=65536
55
  )
56
- output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
57
 
58
- # parsing thinking content
59
- try:
60
- # rindex finding 151668 (</think>)
61
- index = len(output_ids) - output_ids[::-1].index(151668)
62
- except ValueError:
63
- index = 0
64
-
65
- thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
66
- content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
67
-
68
- print("thinking content:", thinking_content) # no opening <think> tag
69
  print("content:", content)
 
70
  """
71
- ....will update later...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  """
73
  ~~~
74
 
 
37
  )
38
 
39
  # prepare the model input
40
+ prompt = "Write a quick sort algorithm."
41
  messages = [
42
  {"role": "user", "content": prompt}
43
  ]
 
53
  **model_inputs,
54
  max_new_tokens=65536
55
  )
56
+ output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
57
 
58
+ content = tokenizer.decode(output_ids, skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
59
  print("content:", content)
60
+
61
  """
62
+ content: Here's a quicksort algorithm implementation in Python:
63
+
64
+ ```python
65
+ def quicksort(arr):
66
+ """
67
+ Sorts an array using the quicksort algorithm.
68
+
69
+ Args:
70
+ arr: List of comparable elements
71
+
72
+ Returns:
73
+ None (sorts in-place)
74
+ """
75
+ if len(arr) <= 1:
76
+ return
77
+
78
+ def partition(low, high):
79
+ """Partition function using the last element as pivot"""
80
+ pivot = arr[high]
81
+ i = low - 1 # Index of smaller element
82
+
83
+ for j in range(low, high):
84
+ if arr[j] <= pivot:
85
+ i += 1
86
+ arr[i], arr[j] = arr[j], arr[i] # Swap elements
87
+
88
+ arr[i + 1], arr[high] = arr[high], arr[i + 1] # Place pivot in correct position
89
+ return i + 1
90
+
91
+ def quicksort_helper(low, high):
92
+ """Recursive helper function"""
93
+ if low < high:
94
+ # Partition the array and get pivot index
95
+ pi = partition(low, high)
96
+
97
+ # Recursively sort elements before and after partition
98
+ quicksort_helper(low, pi - 1)
99
+ quicksort_helper(pi + 1, high)
100
+
101
+ quicksort_helper(0, len(arr) - 1)
102
+
103
+ # Example usage:
104
+ if __name__ == "__main__":
105
+ # Test the algorithm
106
+ test_array = [64, 34, 25, 12, 22, 11, 90]
107
+ print("Original array:", test_array)
108
+
109
+ quicksort(test_array)
110
+ print("Sorted array:", test_array)
111
+
112
+ # Test with other examples
113
+ test_cases = [
114
+ [5, 2, 8, 1, 9],
115
+ [1],
116
+ [],
117
+ [3, 3, 3, 3],
118
+ [5, 4, 3, 2, 1]
119
+ ]
120
+
121
+ for i, case in enumerate(test_cases):
122
+ original = case.copy()
123
+ quicksort(case)
124
+ print(f"Test {i+1}: {original} → {case}")
125
+
126
+ **How it works:**
127
+
128
+ 1. **Divide**: Choose a "pivot" element and partition the array so that elements smaller than the pivot are on the left, and larger elements are on the right.
129
+
130
+ 2. **Conquer**: Recursively apply quicksort to the sub-arrays on both sides of the pivot.
131
+
132
+ 3. **Combine**: Since we're sorting in-place, no additional combining step is needed.
133
+
134
+ **Key features:**
135
+ - **Time Complexity**: O(n log n) average case, O(n²) worst case
136
+ - **Space Complexity**: O(log n) due to recursion stack
137
+ - **In-place sorting**: Modifies the original array
138
+ - **Not stable**: Relative order of equal elements may change
139
+
140
+ **Alternative version with random pivot selection** (better average performance):
141
+
142
+ ```python
143
+ import random
144
+
145
+ def quicksort_random(arr):
146
+ """Quicksort with random pivot selection for better average performance"""
147
+ def partition(low, high):
148
+ # Randomly select pivot and swap with last element
149
+ random_index = random.randint(low, high)
150
+ arr[random_index], arr[high] = arr[high], arr[random_index]
151
+
152
+ pivot = arr[high]
153
+ i = low - 1
154
+
155
+ for j in range(low, high):
156
+ if arr[j] <= pivot:
157
+ i += 1
158
+ arr[i], arr[j] = arr[j], arr[i]
159
+
160
+ arr[i + 1], arr[high] = arr[high], arr[i + 1]
161
+ return i + 1
162
+
163
+ def quicksort_helper(low, high):
164
+ if low < high:
165
+ pi = partition(low, high)
166
+ quicksort_helper(low, pi - 1)
167
+ quicksort_helper(pi + 1, high)
168
+
169
+ if len(arr) > 1:
170
+ quicksort_helper(0, len(arr) - 1)
171
+
172
+ The algorithm efficiently sorts arrays by repeatedly dividing them into smaller subproblems, making it one of the most widely used sorting algorithms in practice.
173
  """
174
  ~~~
175