ObjectScript code examples help
Trying to gather basic code examples written in ObjectScript and realized it's probably easiest to ask the people who know what they're doing!
I'm looking for examples of the following five prompts all in ObjectScript:
Prompt 1. Write a function that partitions the array into two subarrays: one with all even integers, and the other with all odd integers. Return your result in the following format:
[[evens], [odds]]Prompt 2. Create a function that computes the hamming distance between two strings
Prompt 3. Make a function that encrypts a given input with these steps:
Input: "apple"
Step 1: Reverse the input: "elppa"
Step 2: Replace all vowels using the following chart:
a => 0
e => 1
i => 2
o => 2
u => 3
// "1lpp0"Step 3: Add "aca" to the end of the word: "1lpp0aca"
Output: "1lpp0aca"
Examples
encrypt("banana") ➞ "0n0n0baca"
encrypt("karaca") ➞ "0c0r0kaca"
encrypt("burak") ➞ "k0r3baca"
encrypt("alpaca") ➞ "0c0pl0aca"Prompt 4. Write a function that returns true if all characters in a string are identical and false otherwise.
Examples
isIdentical("aaaaaa") ➞ true
isIdentical("aabaaa") ➞ false
isIdentical("ccccca") ➞ false
isIdentical("kk") ➞ truePrompt 5. Create a function that takes a string and returns a string in which each character is repeated once.
Any other examples of basic coding challenges you can think of would also be appreciated!
Thank you!
Comments
Class DC.Samples Extends%RegisteredObject
{
/// partition an array into two subarrays/// return [[even], [odd]]ClassMethod Task1a(xAs%DynamicArray) As%DynamicArray
{
set t(0)=[],t(1)=[]
for i=0:1:x.%Size()-1do t(x.%Get(i)#2).%Push(x.%Get(i))
quit [(t(0)),(t(1))]
}
/// partition an array into two subarrays/// return [[even], [odd]]ClassMethod Task1b(xAs%DynamicArray) As%DynamicArray
{
set t(0)=[], t(1)=[], i=x.%GetIterator()
while i.%GetNext(,.v) { do t(v#2).%Push(v) }
quit [(t(0)),(t(1))]
}
/// hamming distance of two stringsClassMethod Task2(xAs%String, y As%String) As%Integer
{
if$l(x)-$l(y) quit"<Error>"// strings have to be the same lengthsetr=0for i=1:1:$l(x) setr=$e(x,i)'=$e(y,i)+rquitr
}
/// encrypt an stringClassMethod Task3(xAs%String) As%String
{
quit$tr($re(x),"aeiou","01223")_"aca"
}
/// check a string for identical charsClassMethod Task4(xAs%String) As%Boolean
{
quit$tr(x,$e(x))=""
}
/// double charsClassMethod Task5(xAs%String) As%String
{
f i=$l(x):-1:1s$e(x,i)=$e(x,i)_$e(x,i)
qx
}
}
Thanks @Julius Kavay!
For Task2 (hamming distance) and Task5 (double chars) there is also a solution without using a (for, while, etc.) loop .