Skip to content

Commit f17ae5c

Browse files
author
abdul awal
committed
added previous files
1 parent ff9d621 commit f17ae5c

10 files changed

+417
-3
lines changed

another_fiel_for_test.dart

Lines changed: 0 additions & 3 deletions
This file was deleted.

basic_calculator.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
int calculate(String s) {
3+
int ans = 0;
4+
int num = 0;
5+
int sign = 1;
6+
List<int> stack = [];
7+
stack.add(sign);
8+
9+
for (int i = 0; i < s.length; i++) {
10+
final char = s[i];
11+
12+
if (RegExp(r'\d').hasMatch(char)) {
13+
num = num * 10 + int.parse(char);
14+
} else if (char == '(') {
15+
stack.add(sign);
16+
} else if (char == ')') {
17+
stack.removeLast();
18+
} else if (char == '+' || char == '-') {
19+
ans += sign * num;
20+
sign = (char == '+' ? 1 : -1) * stack.last;
21+
num = 0;
22+
}
23+
}
24+
25+
return ans + sign * num;
26+
}
27+
}
28+
29+
void main() {
30+
Solution sl = Solution();
31+
32+
print(sl.calculate('(1+(4+5+2)-3)+(6+8)'));
33+
}

binary_divisibility.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void main(){
2+
3+
final num = int.parse('1111', radix: 2);
4+
print(num);
5+
}

elder.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def elder_age(m, n, l, t):
2+
# arithmic series sum
3+
def s(a1, a2):
4+
return 0 if a1 > a2 else (a1 + a2) * (a2 - a1 + 1) // 2
5+
# edge till 2^k
6+
def edge(x):
7+
ret = 1
8+
while ret < x: ret *= 2
9+
# print(ret)
10+
return ret
11+
12+
m, n = min(m , n), max(m, n)
13+
em, en = edge(m), edge(n)
14+
if m == 0 or l >= en - 1: return 0
15+
if em == en:
16+
return (s(0, em - l - 1) * (m + n - em) + elder_age(em - m, en - n, l, t)) % t
17+
else:
18+
em = en // 2
19+
goal_s1 = s(0, en - l - 1) * m
20+
s1_small = (en - n) * s(max (em - l, 0), en - l - 1)
21+
if l <= em: # no +0s, true FANS /O-O\
22+
small = elder_age(em - m, en - n, 0, t) + (em - l) * (em - m) * (en - n)
23+
else: # with some +0s, fake FANS
24+
small = elder_age(em - m, en - n, l - em, t)
25+
return (goal_s1 - s1_small + small) % t
26+
27+
print(elder_age(115,510,291,255))
28+
29+
#m = 5, n = 5, l = 1, t = 25

immortal_problem.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int elderAge(int n, int m, int k, int newp) {
2+
// Step 1: Generate the initial grid based on XOR of row and column indices
3+
int sum = 0;
4+
5+
// Compute the sum directly without creating the grid
6+
for (int i = 0; i < n; i++) {
7+
for (int j = 0; j < m; j++) {
8+
int value = i ^ j;
9+
int adjustedValue = value - k;
10+
if (adjustedValue < 0) {
11+
adjustedValue = 0;
12+
}
13+
sum += adjustedValue;
14+
}
15+
}
16+
17+
// Handle the time constraint
18+
return sum % newp;
19+
20+
}
21+
22+
void main() {
23+
// Example usage
24+
print(elderAge(5, 8, 1, 100)); // Output: 5
25+
}

mathematical_expression.dart

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import 'dart:collection';
2+
3+
double evaluate(String expression) {
4+
// Helper function to evaluate simple expressions without parentheses
5+
double evalSimple(String exp) {
6+
// Use a queue to process operators with the correct precedence
7+
Queue<String> tokens = Queue.from(exp.split(RegExp(r'(\D)')).where((e) => e.isNotEmpty));
8+
Queue<String> operators = Queue.from(exp.split(RegExp(r'(\d+\.?\d*)')).where((e) => e.isNotEmpty));
9+
10+
// Function to perform an operation
11+
double operate(double left, String operator, double right) {
12+
switch (operator) {
13+
case '+':
14+
return left + right;
15+
case '-':
16+
return left - right;
17+
case '*':
18+
return left * right;
19+
case '/':
20+
return left / right;
21+
default:
22+
throw ArgumentError('Unknown operator: $operator');
23+
}
24+
}
25+
26+
// Stack to handle multiplication and division first
27+
Queue<double> values = Queue<double>();
28+
values.add(double.parse(tokens.removeFirst()));
29+
30+
while (tokens.isNotEmpty) {
31+
String operator = operators.removeFirst();
32+
double value = double.parse(tokens.removeFirst());
33+
34+
if (operator == '*' || operator == '/') {
35+
double prevValue = values.removeLast();
36+
values.add(operate(prevValue, operator, value));
37+
} else {
38+
values.add(value);
39+
operators.addFirst(operator);
40+
}
41+
}
42+
43+
double result = values.removeFirst();
44+
45+
while (operators.isNotEmpty) {
46+
String operator = operators.removeFirst();
47+
double value = values.removeFirst();
48+
result = operate(result, operator, value);
49+
}
50+
51+
return result;
52+
}
53+
54+
// Helper function to handle parentheses
55+
double evalParentheses(String exp) {
56+
while (exp.contains('(')) {
57+
exp = exp.replaceAllMapped(RegExp(r'\(([^()]+)\)'), (match) {
58+
return evalSimple(match.group(1)!).toString();
59+
});
60+
}
61+
return evalSimple(exp);
62+
}
63+
64+
return evalParentheses(expression.replaceAll(' ', ''));
65+
}
66+
67+
void main() {
68+
// Test cases
69+
print(evaluate("1-1")); // Output: 0
70+
print(evaluate("1 -1")); // Output: 0
71+
print(evaluate("1- -1")); // Output: 2
72+
print(evaluate("6 + -(4)")); // Output: 2
73+
print(evaluate("(2 / (2 + 3.33) * 4) - -6")); // Output: 7.2009...
74+
}

matrix_script.dart

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//https://www.hackerrank.com/challenges/matrix-script/problem
2+
import 'dart:core';
3+
4+
void main() {
5+
// List<String> matrix = [
6+
// //5 9
7+
// '#%\$r%r\$n ',
8+
// 'O%Mi\$iTi\$',
9+
// 'yiaxsprt ',
10+
// 'est%ctiy#',
11+
// ' t c i %',
12+
// ];
13+
//Output: #Oye is Mattrix sccript Triinity $ #%
14+
15+
16+
List<String> matrix = [
17+
//7 3
18+
'Tsi',
19+
'h%x',
20+
'i #',
21+
'sM ',
22+
'\$a ',
23+
'#t%',
24+
'ir!',
25+
];
26+
//Output: This is Matrix# %!
27+
28+
// List<String> matrix = [
29+
// //4,6
30+
// 'T%Mic&',
31+
// 'h%axr%',
32+
// 'iit#p!',
33+
// 'ssrst&'
34+
// ];
35+
36+
//output: This isMatrix scrpt&%!&
37+
38+
// List<String> matrix =[
39+
// ///4 6
40+
// '#%\$r%r',
41+
// 'I%Mi\$i',
42+
// 'tiaxsp',
43+
// '#st%ct',
44+
// ];
45+
// //Output: #It is Matrix script
46+
47+
//Input
48+
//4 8
49+
// List<String> matrix = [
50+
// '#%\$r%r\$n',
51+
// 'I%Mi\$iTi',
52+
// 'tiaxsprt',
53+
// '#st%ctiy',
54+
// ];
55+
// //Output: #It is Matrix script Trinity
56+
57+
int row = 7, column = 3;
58+
print(matrixScript(row, column, matrix));
59+
}
60+
61+
String matrixScript(int rows, int column, List<String> matrix) {
62+
// Rotate the matrix
63+
List<List<String>> rotatedMatrix =
64+
List.generate(column, (i) => List.filled(rows, ''));
65+
for (int i = 0; i < rows; i++) {
66+
for (int j = 0; j < column; j++) {
67+
rotatedMatrix[j][i] = matrix[i][j];
68+
}
69+
}
70+
71+
72+
// Flatten the rotated matrix into a single string
73+
StringBuffer sample = StringBuffer();
74+
75+
for (List<String> subset in rotatedMatrix) {
76+
for (String letter in subset) {
77+
sample.write(letter);
78+
}
79+
}
80+
print(sample.toString());
81+
82+
// Replace invalid characters with a space
83+
String result = sample
84+
.toString()
85+
.replaceAllMapped(RegExp(r'(?<=\w)([^\d\w]+)(?=\w)'), (match) => ' ');
86+
87+
return result;
88+
}

merging_sorted_list_node.dart

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
void main() {
2+
3+
print("SOmethig");
4+
// Example usage :
5+
6+
ListNode? list1 =
7+
ListNode(1, ListNode(4, ListNode(5, ListNode(7, ListNode(100)))));
8+
ListNode? list2 =
9+
ListNode(0, ListNode(8, ListNode(50, ListNode(70, ListNode(100)))));
10+
ListNode? list3 =
11+
ListNode(10, ListNode(40, ListNode(50, ListNode(70, ListNode(100)))));
12+
13+
ListNode? list4 = ListNode(
14+
100, ListNode(400, ListNode(500, ListNode(700, ListNode(1000)))));
15+
16+
17+
18+
List<ListNode?> lists = [list1,list2, list3, list4];
19+
20+
Solution solution = Solution();
21+
ListNode? mergedList = solution.mergingSortedList(lists);
22+
23+
while(mergedList != null){
24+
print(mergedList.val);
25+
mergedList = mergedList.next;
26+
}
27+
}
28+
29+
class ListNode {
30+
int val;
31+
ListNode? next;
32+
ListNode([this.val = 0, this.next]);
33+
}
34+
35+
class Solution {
36+
ListNode? mergingSortedList(List<ListNode?> lists) {
37+
ListNode dummy = ListNode(0);
38+
ListNode curr = dummy;
39+
40+
// storing data into heap
41+
42+
List<ListNode> headData= [];
43+
44+
for(final list in lists){
45+
if(list !=null){
46+
headData.add(list);
47+
}
48+
49+
}
50+
51+
headData.sort((a,b) => a.val.compareTo(b.val));
52+
53+
54+
while(headData.isNotEmpty){
55+
final currentSmallVal = headData.removeAt(0);
56+
57+
if(currentSmallVal.next!=null){
58+
headData.add(currentSmallVal.next!);
59+
60+
headData.sort((a,b) => a.val.compareTo(b.val));
61+
62+
}
63+
64+
curr.next = currentSmallVal;
65+
curr = curr.next!;
66+
67+
}
68+
return dummy.next;
69+
70+
}
71+
}

test_file.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
void main() {
2+
List<String> matrix =[
3+
///4 6
4+
'#%\$r%r',
5+
'I%Mi\$i',
6+
'tiaxsp',
7+
'#st%ct',
8+
];
9+
// //Output: #It is Matrix script
10+
11+
12+
matrixScript(4, 6, matrix);
13+
}
14+
15+
String matrixScript(int row, int column, List<String> matrix) {
16+
//rotate the matrix
17+
List<List<String>> rotatedMatrix = List.generate(column, (i)=>List.filled(row, ''));
18+
19+
for(int i =0; i<row; i++){
20+
for(int j = 0; j<column; j++){
21+
rotatedMatrix[j][i] = matrix[i][j];
22+
}
23+
}
24+
//list data
25+
26+
StringBuffer dummy = StringBuffer();
27+
for(List<String> subset in rotatedMatrix){
28+
for(String char in subset){
29+
dummy.write(char);
30+
}
31+
}
32+
33+
print(dummy.toString());
34+
//filter charactes and paces
35+
String result = dummy.toString().replaceAllMapped(RegExp(r'(?<=\w)([^\d\w]+)(?=\w)'), (replace)=>' ');
36+
print(result);
37+
return '';
38+
}

0 commit comments

Comments
 (0)