diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..ed99db1 Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..699a302 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +challange3/.env diff --git a/challange1/index.html b/challange1/index.html new file mode 100644 index 0000000..db31faf --- /dev/null +++ b/challange1/index.html @@ -0,0 +1,115 @@ + + + + + + Approximate Search + + + +

Approximate Search

+

Enter a word to find its matching words

+
+ + + +

Suggestions:

+ +
+ + + + diff --git a/challange1/server.js b/challange1/server.js new file mode 100644 index 0000000..1d45f0b --- /dev/null +++ b/challange1/server.js @@ -0,0 +1,60 @@ +const http = require('http'); +const fs = require('fs'); +const url = require('url'); + +let words = []; + +// Load words from a file +fs.readFile('words.txt', 'utf8', (err, data) => { + if (err) { + console.error("Failed to load words.txt:", err); + return; + } + words = data.split(/\r?\n/).filter(Boolean); +}); + +// Function to calculate Levenshtein Distance +function levenshteinDistance(a, b) { + const dp = Array.from({ length: a.length + 1 }, () => Array(b.length + 1).fill(0)); + + for (let i = 0; i <= a.length; i++) dp[i][0] = i; + for (let j = 0; j <= b.length; j++) dp[0][j] = j; + + for (let i = 1; i <= a.length; i++) { + for (let j = 1; j <= b.length; j++) { + dp[i][j] = Math.min( + dp[i - 1][j] + 1, + dp[i][j - 1] + 1, + dp[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1) + ); + } + } + return dp[a.length][b.length]; +} + +// Approximate search +function approximateSearch(input, k = 3) { + return words + .map(word => ({ word, distance: levenshteinDistance(input, word) })) + .sort((a, b) => a.distance - b.distance) + .slice(0, k) + .map(entry => entry.word); +} + +const server = http.createServer((req, res) => { + const queryObject = url.parse(req.url, true).query; + + if (req.url.startsWith('/search')) { + const input = queryObject.q || ''; + const suggestions = approximateSearch(input); + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(suggestions)); + } else { + res.writeHead(200, { 'Content-Type': 'text/html' }); + fs.createReadStream('index.html').pipe(res); + } +}); + +server.listen(3000, () => { + console.log('Server running at http://localhost:3000/'); +}); diff --git a/challange1/words.txt b/challange1/words.txt new file mode 100644 index 0000000..6f6544a --- /dev/null +++ b/challange1/words.txt @@ -0,0 +1,5 @@ +absolutely +active +activist +activity + diff --git a/challange2/public/index.html b/challange2/public/index.html new file mode 100644 index 0000000..76274c4 --- /dev/null +++ b/challange2/public/index.html @@ -0,0 +1,110 @@ + + + + + + Arithmetic Expression Solver + + + +

Arithmetic Expression Solver

+
+
+ + + +
+
+ Once the result is computed, the downloadable file will appear here. +
+
+ + + diff --git a/challange2/public/script.js b/challange2/public/script.js new file mode 100644 index 0000000..f238fa0 --- /dev/null +++ b/challange2/public/script.js @@ -0,0 +1,3 @@ +document.getElementById('uploadForm').addEventListener('submit', () => { + document.getElementById('result').innerHTML = '

Processing your file...

'; +}); diff --git a/challange2/public/style.css b/challange2/public/style.css new file mode 100644 index 0000000..4d15cea --- /dev/null +++ b/challange2/public/style.css @@ -0,0 +1,54 @@ +body { + font-family: Arial, sans-serif; + background-color: #f9f9f9; + margin: 0; + padding: 0; + display: flex; + justify-content: center; + align-items: center; + height: 100vh; +} + +.container { + background: #fff; + padding: 20px; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + text-align: center; + max-width: 400px; + width: 100%; +} + +h1 { + margin-bottom: 20px; + color: #333; +} + +label { + display: block; + margin-bottom: 8px; + color: #555; +} + +input[type="file"] { + margin-bottom: 20px; +} + +button { + background: #007bff; + color: #fff; + border: none; + padding: 10px 15px; + border-radius: 5px; + cursor: pointer; + font-size: 16px; +} + +button:hover { + background: #0056b3; +} + +#result { + margin-top: 20px; + color: #666; +} diff --git a/challange2/server.js b/challange2/server.js new file mode 100644 index 0000000..aaeba5b --- /dev/null +++ b/challange2/server.js @@ -0,0 +1,64 @@ +const express = require('express'); +const multer = require('multer'); +const fs = require('fs'); +const path = require('path'); + +const app = express(); +const upload = multer({ dest: 'uploads/' }); +const PORT = 3000; + +app.use(express.static(path.join(__dirname, 'public'))); + +function evaluateExpression(expression) { + try { + const safeExpression = expression.replace(/\^/g, '**'); + return eval(safeExpression); + } catch (error) { + return `Error: Invalid expression`; + } +} + +app.post('/process-file', upload.single('file'), (req, res) => { + const inputFile = req.file.path; + const outputFile = path.join(__dirname, 'uploads', `output-${Date.now()}.txt`); + + const inputData = fs.readFileSync(inputFile, 'utf-8'); + const lines = inputData.split('\n'); + + + const results = lines.map((line) => { + const match = line.match(/^(.*)=\s*(.*)$/); + if (match) { + const expression = match[1].trim(); + const providedResult = match[2].trim(); + + try { + const calculatedResult = evaluateExpression(expression); + if (providedResult) { + return calculatedResult == providedResult + ? `${line.trim()}` + : `${line.trim()} Error: Incorrect result (expected ${calculatedResult})`; + } else { + return `${expression} = ${calculatedResult}`; + } + } catch (error) { + return `${line.trim()} Error: ${error.message}`; + } + } else { + return `${line.trim()} Error: Invalid format`; + } + }); + + + + fs.writeFileSync(outputFile, results.join('\n')); + + res.download(outputFile, 'output.txt', () => { + fs.unlinkSync(inputFile); + fs.unlinkSync(outputFile); + }); +}); + +app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +}); diff --git a/challange3/public/index.html b/challange3/public/index.html new file mode 100644 index 0000000..0502c3c --- /dev/null +++ b/challange3/public/index.html @@ -0,0 +1,85 @@ + + + + + + Email Sender + + + +

Email Sender

+

Fill in your details and upload an image to send an email to hr@ignitershub.com.

+
+ + + + + + + + + + + + + + + + +
+ + diff --git a/challange3/server.mjs b/challange3/server.mjs new file mode 100644 index 0000000..43b52f3 --- /dev/null +++ b/challange3/server.mjs @@ -0,0 +1,73 @@ +import express from 'express'; +import multer from 'multer'; +import nodemailer from 'nodemailer'; +import path from 'path'; +import dotenv from 'dotenv'; +import fs from 'fs'; + +dotenv.config(); + +const app = express(); +const PORT = 3000; + +app.use(express.static(path.join(process.cwd(), 'public'))); +app.get('/', (req, res) => { + res.sendFile(path.join(process.cwd(), 'public', 'index.html')); +}); + +// Multer setup +const upload = multer({ + dest: 'uploads/', + fileFilter: (req, file, cb) => { + const allowedTypes = ['image/png', 'image/jpg', 'image/jpeg']; + if (!allowedTypes.includes(file.mimetype)) { + return cb(new Error('Only PNG, JPG, and JPEG files are allowed.')); + } + cb(null, true); + }, +}); + +const transporter = nodemailer.createTransport({ + service: 'gmail', + auth: { + user: process.env.EMAIL_USER, + pass: process.env.EMAIL_PASS, + }, +}); + +app.post('/send-email', upload.single('image'), (req, res) => { + const { name, semester, branch, rollNumber } = req.body; + const { file } = req; + + if (!file) { + return res.status(400).send('Image attachment is required.'); + } + + const mailOptions = { + from: process.env.EMAIL_USER, + to: 'hr@ignitershub.com', + subject: 'Challenge 3 Completed', + text: `Hello,\n\nMy name is ${name}, and here are my details:\nSemester: ${semester}\nBranch: ${branch}\nRoll Number: ${rollNumber}\n\nBest regards,\n${name}`, + attachments: [ + { + filename: file.originalname, + path: path.join(process.cwd(), file.path), + }, + ], + }; + + transporter.sendMail(mailOptions, (error, info) => { + fs.unlinkSync(path.join(process.cwd(), file.path)); + + if (error) { + console.error('Error sending email:', error); + alert('An error occurred while sending the email.'); + } + console.log('Email sent:', info.response); + alert('Email sent successfully!'); + }); +}); + +app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +}); diff --git a/challange4/index.html b/challange4/index.html new file mode 100644 index 0000000..79cbcc7 --- /dev/null +++ b/challange4/index.html @@ -0,0 +1,95 @@ + + + + + + Palindrome Checker + + + +

Palindrome Checker

+
+

Enter a string to check if it's a palindrome:

+ +
+ +
+
+ + + +