Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes: #176 (This issue has been closed earlier but wasn't resolved). Now migrated to null safety, upgraded dart sdk, tested all the changes. #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion conversions/Decimal_To_Any.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ String decimalToAny(int value, int base) {
int remainder = value % base;
value = value ~/ base;
output =
(remainder < 10 ? remainder.toString() : ALPHABET_VALUES[remainder]) +
(remainder < 10 ? remainder.toString() : ALPHABET_VALUES[remainder])! +
output;
}

Expand Down
1 change: 1 addition & 0 deletions conversions/Decimal_To_Binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void bitwiseConversion(var n) {
print("Bitwise conversion.\n");
while (n != 0) {
d = (n & 1);

b += d * (pow(10, c++).toInt());
n >>= 1;
}
Expand Down
4 changes: 2 additions & 2 deletions conversions/Decimal_to_Hexadecimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ String decimal_to_hexadecimal(int decimal_val) {
}
String hex_string = "";
while (decimal_val > 0) {
String hex_val = "";
String? hex_val = "";
int remainder = decimal_val % 16;
decimal_val = decimal_val ~/ 16;
if (hex_table.containsKey(remainder.toString())) {
hex_val = hex_table[remainder.toString()];
} else {
hex_val = remainder.toString();
}
hex_string = hex_val + hex_string;
hex_string = hex_val! + hex_string;
}
return negative ? "-" + hex_string : hex_string;
}
2 changes: 1 addition & 1 deletion conversions/Integer_To_Roman.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ List<String> RomanNumbers = [
"I"
];

List<String> integer_to_roman(int num) {
List<String>? integer_to_roman(int num) {
if (num < 0) {
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions conversions/binary_to_decimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:test/test.dart';

int binaryToDecimal(String binaryString) {
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString == "") {
throw FormatException("An empty value was passed to the function");
}
bool isNegative = binaryString[0] == "-";
Expand All @@ -14,8 +14,8 @@ int binaryToDecimal(String binaryString) {
if ("01".contains(binaryString[i]) == false) {
throw FormatException("Non-binary value was passed to the function");
} else {
decimalValue +=
pow(2, binaryString.length - i - 1) * int.parse((binaryString[i]));
decimalValue += pow(2, binaryString.length - i - 1) *
int.parse((binaryString[i])) as int;
}
}
return isNegative ? -1 * decimalValue : decimalValue;
Expand Down
4 changes: 2 additions & 2 deletions conversions/binary_to_hexadecimal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Map<String, String> hexTable = {
String binaryToHexadecimal(String binaryString) {
// checking for unexpected values
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString == "") {
throw new FormatException("An empty value was passed to the function");
}
try {
Expand All @@ -47,7 +47,7 @@ String binaryToHexadecimal(String binaryString) {
int i = 0;
while (i != binaryString.length) {
String bin_curr = binaryString.substring(i, i + 4);
hexValue += hexTable[bin_curr];
hexValue += hexTable[bin_curr]!;
i += 4;
}

Expand Down
2 changes: 1 addition & 1 deletion conversions/binary_to_octal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {

String binaryToOctal(String binaryString) {
binaryString = binaryString.trim();
if (binaryString == null || binaryString == "") {
if (binaryString == "") {
throw new FormatException("An empty value was passed to the function");
}
bool isNegative = binaryString[0] == "-";
Expand Down
4 changes: 2 additions & 2 deletions conversions/hexadecimal_to_binary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Map<String, String> bin_table = {
String hexadecimal_to_binary(String hex_value) {
// checking for unexpected values
hex_value = hex_value.trim();
if (hex_value == null || hex_value == "") {
if (hex_value == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -40,7 +40,7 @@ String hexadecimal_to_binary(String hex_value) {
if (!bin_table.containsKey(hex_cur)) {
throw new FormatException("An invalid value was passed to the function");
}
bin_val += bin_table[hex_cur];
bin_val += bin_table[hex_cur]!;
i++;
}

Expand Down
11 changes: 8 additions & 3 deletions conversions/hexadecimal_to_decimal.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: deprecated_member_use

import "dart:math" show pow;

Map<String, int> hex_table = {
Expand All @@ -21,19 +23,22 @@ void main() {

int hexadecimal_to_decimal(String hex_string) {
hex_string = hex_string.trim().toUpperCase();
if (hex_string == null || hex_string == "") {
if (hex_string == "") {
throw Exception("An empty value was passed to the function");
}
bool is_negative = hex_string[0] == "-";
if (is_negative) hex_string = hex_string.substring(1);
int decimal_val = 0;
for (int i = 0; i < hex_string.length; i++) {
if (int.parse(hex_string[i], onError: (e) => null) == null &&
if (int.parse(hex_string[i],
onError: ((e) => null) as int Function(String)?) ==
null &&
hex_table.containsKey(hex_string[i]) == false) {
throw Exception("Non-hex value was passed to the function");
} else {
decimal_val += pow(16, hex_string.length - i - 1) *
int.parse((hex_string[i]), onError: (e) => hex_table[hex_string[i]]);
int.parse((hex_string[i]),
onError: (e) => hex_table[hex_string[i]]!) as int;
}
}
return is_negative ? -1 * decimal_val : decimal_val;
Expand Down
18 changes: 9 additions & 9 deletions conversions/hexadecimal_to_octal.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import "dart:math" show pow;

import 'package:test/test.dart';

// Hexadecimal number to octal number conversion program
String hexadecimal_to_octal(String hex_val) {
int dec = 0;

// checking for null string passed to function
if (hex_val == null || hex_val == "") {
if (hex_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -29,43 +30,42 @@ String hexadecimal_to_octal(String hex_val) {
case '7':
case '8':
case '9':
dec = dec + int.parse(ch) * pow(16, c);
dec = dec + int.parse(ch) * (pow(16, c) as int);
c--;
break;
case 'a':
case 'A':
dec = dec + 10 * pow(16, c);
dec = dec + 10 * (pow(16, c) as int);
c--;
break;
case 'b':
case 'B':
dec = dec + 11 * pow(16, c);
dec = dec + 11 * (pow(16, c) as int);
c--;
break;
case 'c':
case 'C':
dec = dec + 12 * pow(16, c);
dec = dec + 12 * (pow(16, c) as int);
c--;
break;
case 'd':
case 'D':
dec = dec + 13 * pow(16, c);
dec = dec + 13 * (pow(16, c) as int);
c--;
break;
case 'e':
case 'E':
dec = dec + 14 * pow(16, c);
dec = dec + 14 * (pow(16, c) as int);
c--;
break;
case 'f':
case 'F':
dec = dec + 15 * pow(16, c);
dec = dec + 15 * (pow(16, c) as int);
c--;
break;
default:
throw new FormatException(
"An invalid value was passed to the function");
break;
}
}

Expand Down
5 changes: 3 additions & 2 deletions conversions/octal_to_binary.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import "dart:math" show pow;

import 'package:test/test.dart';

// octal number to binary number conversion
String ocatal_to_binary(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -31,7 +32,7 @@ String ocatal_to_binary(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0, bin_val = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * (pow(8, i) as int));
i++;
oct = oct ~/ 10;
}
Expand Down
5 changes: 3 additions & 2 deletions conversions/octal_to_decimal.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "dart:math" show pow;

import 'package:test/test.dart';

// octal number to decimal number conversion
Expand All @@ -7,7 +8,7 @@ import 'package:test/test.dart';
String ocatal_to_decimal(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -33,7 +34,7 @@ String ocatal_to_decimal(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * (pow(8, i) as int));
i++;
oct = oct ~/ 10;
}
Expand Down
9 changes: 5 additions & 4 deletions conversions/octal_to_hexadecimal.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "dart:math" show pow;

import 'package:test/test.dart';

// octal number to hex number
Expand All @@ -15,7 +16,7 @@ Map<String, String> hex_table = {
String ocatal_to_hex(String oct_val) {
// checking for unexpected values
oct_val = oct_val.trim();
if (oct_val == null || oct_val == "") {
if (oct_val == "") {
throw new FormatException("An empty value was passed to the function");
}

Expand All @@ -41,7 +42,7 @@ String ocatal_to_hex(String oct_val) {
// converting octal to decimal
int dec_val = 0, i = 0;
while (oct != 0) {
dec_val = dec_val + ((oct % 10) * pow(8, i));
dec_val = dec_val + ((oct % 10) * (pow(8, i) as int));
i++;
oct = oct ~/ 10;
}
Expand All @@ -52,15 +53,15 @@ String ocatal_to_hex(String oct_val) {
}
String hex_string = "";
while (dec_val > 0) {
String hex_val = "";
String? hex_val = "";
int remainder = dec_val % 16;
dec_val = dec_val ~/ 16;
if (hex_table.containsKey(remainder.toString())) {
hex_val = hex_table[remainder.toString()];
} else {
hex_val = remainder.toString();
}
hex_string = hex_val + hex_string;
hex_string = hex_val! + hex_string;
}

// returning the value
Expand Down
35 changes: 18 additions & 17 deletions data_structures/HashMap/Hashing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//Email:[email protected]

class Node {
int data;
Node next;
int? data;
Node? next;

Node(int data) {
this.data = data;
Expand All @@ -12,8 +12,8 @@ class Node {
}

class LinkedList {
Node head;
int size;
Node? head;
int? size;

LinkedList() {
head = null;
Expand All @@ -23,7 +23,7 @@ class LinkedList {
void insert(int data) {
Node newnode = new Node(data);

size++;
if (size != null) size = size! + 1;

if (head == null) {
head = newnode;
Expand All @@ -38,15 +38,16 @@ class LinkedList {
print("underFlow!");
return;
} else {
Node curr = head;
Node curr = head!;
if (curr.data == data) {
head = curr.next;
size--;

if (size != null) size = size! - 1;
return;
} else {
while (curr.next.next != null) {
if (curr.next.data == data) {
curr.next = curr.next.next;
while (curr.next!.next != null) {
if (curr.next!.data == data) {
curr.next = curr.next!.next;
return;
}
}
Expand All @@ -56,7 +57,7 @@ class LinkedList {
}

void display() {
Node temp = head;
Node? temp = head;
while (temp != null) {
print(temp.data.toString());
temp = temp.next;
Expand All @@ -66,11 +67,11 @@ class LinkedList {
}

class HashMap {
int hsize;
List<LinkedList> buckets;
late int hsize;
late List<LinkedList?> buckets = [];

HashMap(int hsize) {
buckets = new List<LinkedList>(hsize);
buckets = new List.filled(hsize, null);
for (int i = 0; i < hsize; i++) {
buckets[i] = new LinkedList();
}
Expand All @@ -87,18 +88,18 @@ class HashMap {

void insertHash(int key) {
int hash = hashing(key);
buckets[hash].insert(key);
buckets[hash]!.insert(key);
}

void deleteHash(int key) {
int hash = hashing(key);
buckets[hash].delete(key);
buckets[hash]!.delete(key);
}

void displayHashtable() {
for (int i = 0; i < hsize; i++) {
print("Bucket $i:");
buckets[i].display();
buckets[i]!.display();
}
}
}
Expand Down
Loading