Reverse Alphabet
Description
Reverse Alphabet is a simple yet powerful tool that takes any individual word as input and promptly returns its reverse.
Using Truebit To Execute Reverse Alphabet
We use Truebit to execute the Reserver alphabet task to get a verified result.
The Code
You can find the Reverse Alphabet example within the folder truebit-nextgen-examples/function-tasks/<language>/reverse-alphabet
📁 src └─ 📄 main.rs 📄 Cargo.toml 📄 Cargo.lock
📁 src └─ 📄 main.c 📄 compile.sh
📁 src └─ 📄 main.cpp 📄 compile.sh
📁 src └─ 📄 main.js 📄 package-lock.json 📄 package.json
📁 src └─ 📄 task.py
Source
main.rs
fn run_task(input: String) -> String {
input.chars().rev().collect()
}
fn main() {
let input: String =
std::fs::read_to_string("input.txt").expect("Could not read from input.txt");
let output = run_task(input);
std::fs::write("output.txt", output).expect("Could not write to output.txt");
}main.c
#include <stdio.h>
#include <stdlib.h>
int fibonacci(int n) {
int a = 0, b = 1, c;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}
int main() {
FILE *inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
perror("Error opening input.txt");
return 1;
}
int input;
if (fscanf(inputFile, "%d", &input) != 1) {
perror("Error reading input.txt");
fclose(inputFile);
return 1;
}
fclose(inputFile);
int result = fibonacci(input);
FILE *outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
perror("Error opening output.txt");
return 1;
}
fprintf(outputFile, "%d", result);
fclose(outputFile);
return 0;
}main.cpp
main.js
In/Out Parameters
In order to send parameters to the Truebit task, you need to open the "input.txt" file and get the value from there. For now, only one input parameter is allowed.
In order to retrieve the output value from the Truebit task, you need save it in the "output.txt" file.
Try Out
We will use the Truebit CLI to compile and try our source code. Once the code is finished, we will deploy it to the coordination hub so that everyone who knows the namespace and taskname.
Step 1: Create The Reverse Alphabet Source Code
Within the src folder you will find a file called main in the case of Rust, C, C++, JS, and task.py in the case of Python. This is the Reverse Alphabet implementation that will be used for testing purpose.
Step 2: Build The Source Code
Execute the build command against Truebit node to get an instrumented Truebit task
Output
Step 3: Try The Code
Execute the start command against the Truebit node to test our Algorithm. You will need to submit the instrumented task id + the input parameter.
[taskId]: Add the taskId generated in the previous step.
Output
Step 4: Deploy the task
Last, but not least, Execute the deploy command to deploy our task to the coordination hub, so that anyone with the namespace, taskname and the API key can execute it.
[taskId]: Add the taskId generated in step 2
Output
Last updated
Was this helpful?