File size: 4,576 Bytes
b578034
 
 
 
 
1
2
3
4
5
6
{"modality": "code+reasoning", "prompt": "Create a Java class which sorts the given array of numbers.\n[9, 2, 4, 3, 6, 1]", "think": "", "answer": "class ArraySort { \n  \n    void sort(int arr[]) { \n        int n = arr.length; \n  \n        // One by one move boundary of unsorted subarray \n        for (int i = 0; i < n-1; i++) { \n            \n            // Find the minimum element in unsorted array \n            int min_index = i; \n            for (int j = i+1; j < n; j++) \n                if (arr[j] < arr[min_index]) \n                    min_index = j; \n  \n            // Swap the found minimum element with the first element \n            int temp = arr[min_index]; \n            arr[min_index] = arr[i]; \n            arr[i] = temp; \n        } \n    } \n  \n    // Prints the array \n    void printArray(int arr[]) { \n        int n = arr.length; \n        for (int i=0; i<n; ++i) \n            System.out.print(arr[i] + \" \"); \n        System.out.println(); \n    } \n  \n    // Driver code to test above \n    public static void main(String args[]) \n    { \n        ArraySort ob = new ArraySort(); \n        int arr[] = {9, 2, 4, 3, 6, 1}; \n        ob.sort(arr); \n        System.out.println(\"Sorted array\"); \n        ob.printArray(arr); \n    } \n}"}
{"modality": "code+reasoning", "prompt": "Create an algorithm in JavaScript to find the closest pair of points from a given set of points.\nlet points = [[2, 0], [4, 4], [-2, 2], [-3, -4], [4, -2]];", "think": "", "answer": "// algorithm to find closest pair of points \n// uses divide and conquer approach \nconst closestPair = (points) => { \n  // base case: two or fewer points\n  if (points.length <= 2) {\n    return points;\n  }\n\n  // divide\n  const middle = Math.floor(points.length / 2);\n  const left = points.slice(0, middle);\n  const right = points.slice(middle);\n  \n  // conquer\n  const leftPoints = closestPair(left);\n  const rightPoints = closestPair(right);\n\n  const closestPair = getClosestPair(leftPoints, rightPoints);\n\n  // combine\n  return closestPair;\n}\n \n// get closest pair between two sets of points\nconst getClosestPair = (leftPoints, rightPoints) => {\n  // calculating the minimum distance\n  let minDist = getDistance(leftPoints[0], rightPoints[0]);\n  let closestPair = [leftPoints[0], rightPoints[0]];\n  \n  for (let i = 0; i < leftPoints.length; i++) {\n    for (let j = 0; j < rightPoints.length; j++) {\n      let dist = getDistance(leftPoints[i], rightPoints[j]);\n      if (dist < minDist) {\n        minDist = dist;\n        closestPair = [leftPoints[i], rightPoints[j]];\n      }\n    }\n  }\n \n  // return the minimum distance and its pair\n  return closestPair;\n}\n \n// calculate the Euclidean distance between two points\nconst getDistance = (point1, point2) => {\n  let xDist = Math.abs(point2[0] - point1[0]);\n  let yDist = Math.abs(point2[1] - point1[1]);\n \n  return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));\n}\n\n// testing our function \nlet points = [[2, 0], [4, 4], [-2, 2], [-3, -4], [4, -2]];\nlet closestPoints = closestPair(points);\nconsole.log(closestPoints);\n//Output: [[-2, 2], [2, 0]]"}
{"modality": "code+reasoning", "prompt": "Change the following loop so that it prints out the numbers from 10 to 1 in descending order.\nfor i in range(1, 11):\n    print(i)", "think": "", "answer": "for i in range(10, 0, -1):\n    print(i)"}
{"modality": "code+reasoning", "prompt": "Write an SQL query that returns the 5 most popular books in the library.\nSELECT * FROM libraryBook", "think": "", "answer": "SELECT bookID, bookTitle, COUNT(*) AS Count \nFROM libraryBook \nGROUP BY bookID, bookTitle \nORDER BY Count DESC \nLIMIT 5;"}
{"modality": "code+reasoning", "prompt": "Train an AI model to predict whether an email is spam or not.\n", "think": "", "answer": "import nltk\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.naive_bayes import MultinomialNB\n\n# loading data\ndata = [('This is an email about about a promotion', 'spam'),\n\t('We want to transfer money to your account', 'spam'),\n\t('This email is about programming', 'not_spam'),\n\t('There is a new version of python', 'not_spam'),\n]\n\n# extracting features\nX, y = [], []\nfor feat, lab in data:\n\tX.append(feat)\n\ty.append(lab)\n\ncv = CountVectorizer()\nX_vect = cv.fit_transform(X)\n\n# training the model\nmodel = MultinomialNB()\nmodel.fit(X_vect, y)\n\n# predicting\nprediction = model.predict(cv.transform([\"This is an email about a discount sale\"]))\nprint(prediction)\n\n# Output\n['spam']"}